annotate Objects/abstract.c @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 74c0a78114ae
children e38335d4df91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Abstract Object Interface (many thanks to Jim Fulton) */
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 #include "Python.h"
8412
4505160a7e5c Address some gcc -Wall warnings (e.g. include <ctype.h>).
Guido van Rossum <guido@python.org>
parents: 8400
diff changeset
4 #include <ctype.h>
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
5 #include "structmember.h" /* we need the offsetof() macro from there */
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
6 #include "longintrepr.h"
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
7
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
8 #define NEW_STYLE_NUMBER(o) PyType_HasFeature((o)->ob_type, \
15944
f464a03be85a Rich comparisons fall-out:
Guido van Rossum <guido@python.org>
parents: 15721
diff changeset
9 Py_TPFLAGS_CHECKTYPES)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
11
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
12 /* Shorthands to return certain errors */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
13
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
14 static PyObject *
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
15 type_error(const char *msg, PyObject *obj)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
16 {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
17 PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
18 return NULL;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
19 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
20
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
21 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
22 null_error(void)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
23 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
24 if (!PyErr_Occurred())
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
25 PyErr_SetString(PyExc_SystemError,
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
26 "null argument to internal routine");
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
27 return NULL;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
28 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
29
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
30 /* Operations on any object */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
31
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
32 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
33 PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
34 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
35 int r;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
36
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
37 if (o1 == NULL || o2 == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
38 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
39 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
40 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
41 r = PyObject_Compare(o1, o2);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
42 if (PyErr_Occurred())
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
43 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
44 *result = r;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
49 PyObject_Type(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 PyObject *v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
53 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
54 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 v = (PyObject *)o->ob_type;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 Py_INCREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 return v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
60 Py_ssize_t
13575
7c119f050290 change abstract size functions PySequence_Size &c.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13451
diff changeset
61 PyObject_Size(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
63 PySequenceMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
65 if (o == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
66 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
67 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
68 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
70 m = o->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
71 if (m && m->sq_length)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
72 return m->sq_length(o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
13575
7c119f050290 change abstract size functions PySequence_Size &c.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13451
diff changeset
74 return PyMapping_Size(o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
77 #undef PyObject_Length
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
78 Py_ssize_t
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
79 PyObject_Length(PyObject *o)
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
80 {
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
81 return PyObject_Size(o);
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
82 }
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
83 #define PyObject_Length PyObject_Size
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
84
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
85
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
86 /* The length hint function returns a non-negative value from o.__len__()
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
87 or o.__length_hint__(). If those methods aren't found or return a negative
51282
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
88 value, then the defaultvalue is returned. If one of the calls fails,
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
89 this function returns -1.
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
90 */
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
91
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
92 Py_ssize_t
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
93 _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35364
diff changeset
94 {
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
95 static PyObject *hintstrobj = NULL;
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
96 PyObject *ro;
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
97 Py_ssize_t rv;
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
98
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
99 /* try o.__len__() */
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
100 rv = PyObject_Size(o);
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
101 if (rv >= 0)
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35364
diff changeset
102 return rv;
51282
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
103 if (PyErr_Occurred()) {
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
104 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
105 !PyErr_ExceptionMatches(PyExc_AttributeError))
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
106 return -1;
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
107 PyErr_Clear();
51282
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
108 }
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35364
diff changeset
109
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
110 /* cache a hashed version of the attribute string */
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
111 if (hintstrobj == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
112 hintstrobj = PyString_InternFromString("__length_hint__");
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
113 if (hintstrobj == NULL)
51282
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
114 return -1;
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35364
diff changeset
115 }
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
116
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
117 /* try o.__length_hint__() */
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
118 ro = PyObject_CallMethodObjArgs(o, hintstrobj, NULL);
51282
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
119 if (ro == NULL) {
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
120 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
121 !PyErr_ExceptionMatches(PyExc_AttributeError))
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
122 return -1;
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
123 PyErr_Clear();
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
124 return defaultvalue;
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
125 }
51466
5849a4f904a2 Merged revisions 69433,69436 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51289
diff changeset
126 rv = PyLong_Check(ro) ? PyLong_AsSsize_t(ro) : defaultvalue;
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
127 Py_DECREF(ro);
51282
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
128 return rv;
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35364
diff changeset
129 }
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35364
diff changeset
130
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
132 PyObject_GetItem(PyObject *o, PyObject *key)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
134 PyMappingMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
135
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
136 if (o == NULL || key == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
137 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
139 m = o->ob_type->tp_as_mapping;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
140 if (m && m->mp_subscript)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
141 return m->mp_subscript(o, key);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
143 if (o->ob_type->tp_as_sequence) {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
144 if (PyIndex_Check(key)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
145 Py_ssize_t key_value;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
146 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
11840
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
147 if (key_value == -1 && PyErr_Occurred())
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
148 return NULL;
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
149 return PySequence_GetItem(o, key_value);
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
150 }
21044
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
151 else if (o->ob_type->tp_as_sequence->sq_item)
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
152 return type_error("sequence index must "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
153 "be integer, not '%.200s'", key);
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
154 }
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
155
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
156 return type_error("'%.200s' object is unsubscriptable", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
160 PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
162 PyMappingMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
164 if (o == NULL || key == NULL || value == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
165 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
166 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
167 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
168 m = o->ob_type->tp_as_mapping;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
169 if (m && m->mp_ass_subscript)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
170 return m->mp_ass_subscript(o, key, value);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
172 if (o->ob_type->tp_as_sequence) {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
173 if (PyIndex_Check(key)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
174 Py_ssize_t key_value;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
175 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
11840
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
176 if (key_value == -1 && PyErr_Occurred())
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
177 return -1;
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
178 return PySequence_SetItem(o, key_value, value);
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
179 }
21044
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
180 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
181 type_error("sequence index must be "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
182 "integer, not '%.200s'", key);
21044
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
183 return -1;
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
184 }
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
185 }
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
186
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
187 type_error("'%.200s' object does not support item assignment", o);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
188 return -1;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
191 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
192 PyObject_DelItem(PyObject *o, PyObject *key)
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
193 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
194 PyMappingMethods *m;
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
195
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
196 if (o == NULL || key == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
197 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
198 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
199 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
200 m = o->ob_type->tp_as_mapping;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
201 if (m && m->mp_ass_subscript)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
202 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
203
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
204 if (o->ob_type->tp_as_sequence) {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
205 if (PyIndex_Check(key)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
206 Py_ssize_t key_value;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
207 key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
11840
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
208 if (key_value == -1 && PyErr_Occurred())
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
209 return -1;
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
210 return PySequence_DelItem(o, key_value);
32f975cb9f5f Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
Andrew M. Kuchling <amk@amk.ca>
parents: 11765
diff changeset
211 }
21044
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
212 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
213 type_error("sequence index must be "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
214 "integer, not '%.200s'", key);
21044
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
215 return -1;
b3d627ee2b73 PyObject_GetItem(), PyObject_SetItem(), PyObject_DelItem(): Fix a few
Guido van Rossum <guido@python.org>
parents: 20946
diff changeset
216 }
8992
ee3af911ad56 Better error messages when a sequence is indexed with a non-integer.
Guido van Rossum <guido@python.org>
parents: 8807
diff changeset
217 }
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
218
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
219 type_error("'%.200s' object does not support item deletion", o);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
220 return -1;
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
221 }
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
222
21644
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
223 int
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
224 PyObject_DelItemString(PyObject *o, char *key)
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
225 {
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
226 PyObject *okey;
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
227 int ret;
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
228
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
229 if (o == NULL || key == NULL) {
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
230 null_error();
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
231 return -1;
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
232 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
233 okey = PyString_FromString(key);
21644
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
234 if (okey == NULL)
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
235 return -1;
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
236 ret = PyObject_DelItem(o, okey);
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
237 Py_DECREF(okey);
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
238 return ret;
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
239 }
3c0bd2f37a78 Implement PyObject_DelItemString. Fixes #498915.
Martin v. Löwis <martin@v.loewis.de>
parents: 21044
diff changeset
240
38632
931ad06bec56 Fix coding style guide bug.
Brett Cannon <bcannon@gmail.com>
parents: 37813
diff changeset
241 int
931ad06bec56 Fix coding style guide bug.
Brett Cannon <bcannon@gmail.com>
parents: 37813
diff changeset
242 PyObject_AsCharBuffer(PyObject *obj,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
243 const char **buffer,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
244 Py_ssize_t *buffer_len)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
245 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
246 PyBufferProcs *pb;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36519
diff changeset
247 char *pp;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
248 Py_ssize_t len;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
249
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
250 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
251 null_error();
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
252 return -1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
253 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
254 pb = obj->ob_type->tp_as_buffer;
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
255 if (pb == NULL ||
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
256 pb->bf_getcharbuffer == NULL ||
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
257 pb->bf_getsegcount == NULL) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
258 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
259 "expected a character buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
260 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
261 }
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
262 if ((*pb->bf_getsegcount)(obj,NULL) != 1) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
263 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
264 "expected a single-segment buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
265 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
266 }
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
267 len = (*pb->bf_getcharbuffer)(obj, 0, &pp);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
268 if (len < 0)
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
269 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
270 *buffer = pp;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
271 *buffer_len = len;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
272 return 0;
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
273 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
274
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
275 int
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
276 PyObject_CheckReadBuffer(PyObject *obj)
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
277 {
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
278 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
279
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
280 if (pb == NULL ||
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
281 pb->bf_getreadbuffer == NULL ||
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
282 pb->bf_getsegcount == NULL ||
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
283 (*pb->bf_getsegcount)(obj, NULL) != 1)
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
284 return 0;
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
285 return 1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
286 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
287
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
288 int PyObject_AsReadBuffer(PyObject *obj,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
289 const void **buffer,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
290 Py_ssize_t *buffer_len)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
291 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
292 PyBufferProcs *pb;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
293 void *pp;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
294 Py_ssize_t len;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
295
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
296 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
297 null_error();
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
298 return -1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
299 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
300 pb = obj->ob_type->tp_as_buffer;
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
301 if (pb == NULL ||
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
302 pb->bf_getreadbuffer == NULL ||
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
303 pb->bf_getsegcount == NULL) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
304 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
305 "expected a readable buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
306 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
307 }
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
308 if ((*pb->bf_getsegcount)(obj, NULL) != 1) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
309 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
310 "expected a single-segment buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
311 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
312 }
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
313 len = (*pb->bf_getreadbuffer)(obj, 0, &pp);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
314 if (len < 0)
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
315 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
316 *buffer = pp;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
317 *buffer_len = len;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
318 return 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
319 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
320
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
321 int PyObject_AsWriteBuffer(PyObject *obj,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
322 void **buffer,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
323 Py_ssize_t *buffer_len)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
324 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
325 PyBufferProcs *pb;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
326 void*pp;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
327 Py_ssize_t len;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
328
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
329 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
330 null_error();
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
331 return -1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
332 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
333 pb = obj->ob_type->tp_as_buffer;
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
334 if (pb == NULL ||
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
335 pb->bf_getwritebuffer == NULL ||
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
336 pb->bf_getsegcount == NULL) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
337 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
338 "expected a writeable buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
339 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
340 }
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
341 if ((*pb->bf_getsegcount)(obj, NULL) != 1) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
342 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
343 "expected a single-segment buffer object");
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
344 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
345 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
346 len = (*pb->bf_getwritebuffer)(obj,0,&pp);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
347 if (len < 0)
20946
ef12c9ddefa7 Add PyObject_CheckReadBuffer(), which returns true if its argument
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20824
diff changeset
348 return -1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
349 *buffer = pp;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
350 *buffer_len = len;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
351 return 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
352 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
353
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
354 /* Buffer C-API for Python 3.0 */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
355
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
356 int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
357 PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
358 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
359 if (!PyObject_CheckBuffer(obj)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
360 PyErr_Format(PyExc_TypeError,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
361 "'%100s' does not have the buffer interface",
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
362 Py_TYPE(obj)->tp_name);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
363 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
364 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
365 return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
366 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
367
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
368 static int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
369 _IsFortranContiguous(Py_buffer *view)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
370 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
371 Py_ssize_t sd, dim;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
372 int i;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
373
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
374 if (view->ndim == 0) return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
375 if (view->strides == NULL) return (view->ndim == 1);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
376
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
377 sd = view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
378 if (view->ndim == 1) return (view->shape[0] == 1 ||
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
379 sd == view->strides[0]);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
380 for (i=0; i<view->ndim; i++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
381 dim = view->shape[i];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
382 if (dim == 0) return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
383 if (view->strides[i] != sd) return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
384 sd *= dim;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
385 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
386 return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
387 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
388
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
389 static int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
390 _IsCContiguous(Py_buffer *view)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
391 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
392 Py_ssize_t sd, dim;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
393 int i;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
394
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
395 if (view->ndim == 0) return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
396 if (view->strides == NULL) return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
397
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
398 sd = view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
399 if (view->ndim == 1) return (view->shape[0] == 1 ||
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
400 sd == view->strides[0]);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
401 for (i=view->ndim-1; i>=0; i--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
402 dim = view->shape[i];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
403 if (dim == 0) return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
404 if (view->strides[i] != sd) return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
405 sd *= dim;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
406 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
407 return 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
408 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
409
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
410 int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
411 PyBuffer_IsContiguous(Py_buffer *view, char fort)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
412 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
413
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
414 if (view->suboffsets != NULL) return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
415
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
416 if (fort == 'C')
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
417 return _IsCContiguous(view);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
418 else if (fort == 'F')
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
419 return _IsFortranContiguous(view);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
420 else if (fort == 'A')
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
421 return (_IsCContiguous(view) || _IsFortranContiguous(view));
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
422 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
423 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
424
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
425
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
426 void*
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
427 PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
428 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
429 char* pointer;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
430 int i;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
431 pointer = (char *)view->buf;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
432 for (i = 0; i < view->ndim; i++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
433 pointer += view->strides[i]*indices[i];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
434 if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
435 pointer = *((char**)pointer) + view->suboffsets[i];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
436 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
437 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
438 return (void*)pointer;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
439 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
440
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
441
46452
91b099a7f3e2 Prevent namespace pollution, add static for internal functions
Neal Norwitz <nnorwitz@gmail.com>
parents: 45936
diff changeset
442 static void
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
443 _add_one_to_index_F(int nd, Py_ssize_t *index, Py_ssize_t *shape)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
444 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
445 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
446
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
447 for (k=0; k<nd; k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
448 if (index[k] < shape[k]-1) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
449 index[k]++;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
450 break;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
451 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
452 else {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
453 index[k] = 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
454 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
455 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
456 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
457
46452
91b099a7f3e2 Prevent namespace pollution, add static for internal functions
Neal Norwitz <nnorwitz@gmail.com>
parents: 45936
diff changeset
458 static void
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
459 _add_one_to_index_C(int nd, Py_ssize_t *index, Py_ssize_t *shape)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
460 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
461 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
462
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
463 for (k=nd-1; k>=0; k--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
464 if (index[k] < shape[k]-1) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
465 index[k]++;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
466 break;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
467 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
468 else {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
469 index[k] = 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
470 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
471 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
472 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
473
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
474 /* view is not checked for consistency in either of these. It is
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
475 assumed that the size of the buffer is view->len in
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
476 view->len / view->itemsize elements.
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
477 */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
478
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
479 int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
480 PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
481 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
482 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
483 void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
484 Py_ssize_t *indices, elements;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
485 char *dest, *ptr;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
486
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
487 if (len > view->len) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
488 len = view->len;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
489 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
490
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
491 if (PyBuffer_IsContiguous(view, fort)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
492 /* simplest copy is all that is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
493 memcpy(buf, view->buf, len);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
494 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
495 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
496
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
497 /* Otherwise a more elaborate scheme is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
498
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
499 /* XXX(nnorwitz): need to check for overflow! */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
500 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
501 if (indices == NULL) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
502 PyErr_NoMemory();
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
503 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
504 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
505 for (k=0; k<view->ndim;k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
506 indices[k] = 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
507 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
508
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
509 if (fort == 'F') {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
510 addone = _add_one_to_index_F;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
511 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
512 else {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
513 addone = _add_one_to_index_C;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
514 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
515 dest = buf;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
516 /* XXX : This is not going to be the fastest code in the world
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
517 several optimizations are possible.
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
518 */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
519 elements = len / view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
520 while (elements--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
521 addone(view->ndim, indices, view->shape);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
522 ptr = PyBuffer_GetPointer(view, indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
523 memcpy(dest, ptr, view->itemsize);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
524 dest += view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
525 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
526 PyMem_Free(indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
527 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
528 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
529
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
530 int
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
531 PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
532 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
533 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
534 void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
535 Py_ssize_t *indices, elements;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
536 char *src, *ptr;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
537
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
538 if (len > view->len) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
539 len = view->len;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
540 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
541
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
542 if (PyBuffer_IsContiguous(view, fort)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
543 /* simplest copy is all that is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
544 memcpy(view->buf, buf, len);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
545 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
546 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
547
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
548 /* Otherwise a more elaborate scheme is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
549
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
550 /* XXX(nnorwitz): need to check for overflow! */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
551 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
552 if (indices == NULL) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
553 PyErr_NoMemory();
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
554 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
555 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
556 for (k=0; k<view->ndim;k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
557 indices[k] = 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
558 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
559
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
560 if (fort == 'F') {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
561 addone = _add_one_to_index_F;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
562 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
563 else {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
564 addone = _add_one_to_index_C;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
565 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
566 src = buf;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
567 /* XXX : This is not going to be the fastest code in the world
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
568 several optimizations are possible.
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
569 */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
570 elements = len / view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
571 while (elements--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
572 addone(view->ndim, indices, view->shape);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
573 ptr = PyBuffer_GetPointer(view, indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
574 memcpy(ptr, src, view->itemsize);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
575 src += view->itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
576 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
577
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
578 PyMem_Free(indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
579 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
580 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
581
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
582 int PyObject_CopyData(PyObject *dest, PyObject *src)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
583 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
584 Py_buffer view_dest, view_src;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
585 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
586 Py_ssize_t *indices, elements;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
587 char *dptr, *sptr;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
588
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
589 if (!PyObject_CheckBuffer(dest) ||
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
590 !PyObject_CheckBuffer(src)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
591 PyErr_SetString(PyExc_TypeError,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
592 "both destination and source must have the "\
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
593 "buffer interface");
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
594 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
595 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
596
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
597 if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
598 if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
599 PyBuffer_Release(&view_dest);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
600 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
601 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
602
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
603 if (view_dest.len < view_src.len) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
604 PyErr_SetString(PyExc_BufferError,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
605 "destination is too small to receive data from source");
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
606 PyBuffer_Release(&view_dest);
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
607 PyBuffer_Release(&view_src);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
608 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
609 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
610
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
611 if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
612 PyBuffer_IsContiguous(&view_src, 'C')) ||
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
613 (PyBuffer_IsContiguous(&view_dest, 'F') &&
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
614 PyBuffer_IsContiguous(&view_src, 'F'))) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
615 /* simplest copy is all that is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
616 memcpy(view_dest.buf, view_src.buf, view_src.len);
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
617 PyBuffer_Release(&view_dest);
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
618 PyBuffer_Release(&view_src);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
619 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
620 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
621
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
622 /* Otherwise a more elaborate copy scheme is needed */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
623
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
624 /* XXX(nnorwitz): need to check for overflow! */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
625 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
626 if (indices == NULL) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
627 PyErr_NoMemory();
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
628 PyBuffer_Release(&view_dest);
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
629 PyBuffer_Release(&view_src);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
630 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
631 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
632 for (k=0; k<view_src.ndim;k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
633 indices[k] = 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
634 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
635 elements = 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
636 for (k=0; k<view_src.ndim; k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
637 /* XXX(nnorwitz): can this overflow? */
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
638 elements *= view_src.shape[k];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
639 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
640 while (elements--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
641 _add_one_to_index_C(view_src.ndim, indices, view_src.shape);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
642 dptr = PyBuffer_GetPointer(&view_dest, indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
643 sptr = PyBuffer_GetPointer(&view_src, indices);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
644 memcpy(dptr, sptr, view_src.itemsize);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
645 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
646 PyMem_Free(indices);
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
647 PyBuffer_Release(&view_dest);
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
648 PyBuffer_Release(&view_src);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
649 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
650 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
651
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
652 void
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
653 PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
654 Py_ssize_t *strides, int itemsize,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
655 char fort)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
656 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
657 int k;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
658 Py_ssize_t sd;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
659
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
660 sd = itemsize;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
661 if (fort == 'F') {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
662 for (k=0; k<nd; k++) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
663 strides[k] = sd;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
664 sd *= shape[k];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
665 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
666 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
667 else {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
668 for (k=nd-1; k>=0; k--) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
669 strides[k] = sd;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
670 sd *= shape[k];
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
671 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
672 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
673 return;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
674 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
675
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
676 int
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
677 PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
678 int readonly, int flags)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
679 {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
680 if (view == NULL) return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
681 if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
682 (readonly == 1)) {
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
683 PyErr_SetString(PyExc_BufferError,
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
684 "Object is not writable.");
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
685 return -1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
686 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
687
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
688 view->obj = obj;
48231
e8509ac501f3 Make obj an owned reference in Py_buffer; this checkin
Martin v. Löwis <martin@v.loewis.de>
parents: 48221
diff changeset
689 if (obj)
e8509ac501f3 Make obj an owned reference in Py_buffer; this checkin
Martin v. Löwis <martin@v.loewis.de>
parents: 48221
diff changeset
690 Py_INCREF(obj);
45832
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
691 view->buf = buf;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
692 view->len = len;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
693 view->readonly = readonly;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
694 view->itemsize = 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
695 view->format = NULL;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
696 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
697 view->format = "B";
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
698 view->ndim = 1;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
699 view->shape = NULL;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
700 if ((flags & PyBUF_ND) == PyBUF_ND)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
701 view->shape = &(view->len);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
702 view->strides = NULL;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
703 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
704 view->strides = &(view->itemsize);
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
705 view->suboffsets = NULL;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
706 view->internal = NULL;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
707 return 0;
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
708 }
5c0b5cabeec9 Finish backporting new buffer API to Python 2.6. Left to do: memoryview object and structmodule. But, these need to be finished in Python 3.0 first. No objects support the new buffer API in Python 2.6 as of yet, and except for the memoryview object, I don't think they will.
Travis E. Oliphant <oliphant@enthought.com>
parents: 45381
diff changeset
709
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
710 void
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
711 PyBuffer_Release(Py_buffer *view)
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
712 {
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
713 PyObject *obj = view->obj;
48236
3650b1ce4bd8 Fix memory leak: Always DECREF obj in PyBuffer_Release.
Martin v. Löwis <martin@v.loewis.de>
parents: 48231
diff changeset
714 if (obj && Py_TYPE(obj)->tp_as_buffer && Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer)
3650b1ce4bd8 Fix memory leak: Always DECREF obj in PyBuffer_Release.
Martin v. Löwis <martin@v.loewis.de>
parents: 48231
diff changeset
715 Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer(obj, view);
3650b1ce4bd8 Fix memory leak: Always DECREF obj in PyBuffer_Release.
Martin v. Löwis <martin@v.loewis.de>
parents: 48231
diff changeset
716 Py_XDECREF(obj);
48231
e8509ac501f3 Make obj an owned reference in Py_buffer; this checkin
Martin v. Löwis <martin@v.loewis.de>
parents: 48221
diff changeset
717 view->obj = NULL;
48221
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
718 }
b8c4bf393794 Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents: 47910
diff changeset
719
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
720 PyObject *
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
721 PyObject_Format(PyObject* obj, PyObject *format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
722 {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
723 static PyObject * str__format__ = NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
724 PyObject *empty = NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
725 PyObject *result = NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
726 int spec_is_unicode;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
727 int result_is_unicode;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
728
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
729 /* Initialize cached value */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
730 if (str__format__ == NULL) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
731 /* Initialize static variable needed by _PyType_Lookup */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
732 str__format__ = PyString_InternFromString("__format__");
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
733 if (str__format__ == NULL)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
734 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
735 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
736
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
737 /* If no format_spec is provided, use an empty string */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
738 if (format_spec == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
739 empty = PyString_FromStringAndSize(NULL, 0);
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
740 format_spec = empty;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
741 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
742
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
743 /* Check the format_spec type, and make sure it's str or unicode */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
744 if (PyUnicode_Check(format_spec))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
745 spec_is_unicode = 1;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
746 else if (PyString_Check(format_spec))
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
747 spec_is_unicode = 0;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
748 else {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
749 PyErr_Format(PyExc_TypeError,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
750 "format expects arg 2 to be string "
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
751 "or unicode, not %.100s", Py_TYPE(format_spec)->tp_name);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
752 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
753 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
754
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
755 /* Make sure the type is initialized. float gets initialized late */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
756 if (Py_TYPE(obj)->tp_dict == NULL)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
757 if (PyType_Ready(Py_TYPE(obj)) < 0)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
758 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
759
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
760 /* Check for a __format__ method and call it. */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
761 if (PyInstance_Check(obj)) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
762 /* We're an instance of a classic class */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
763 PyObject *bound_method = PyObject_GetAttr(obj,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
764 str__format__);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
765 if (bound_method != NULL) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
766 result = PyObject_CallFunctionObjArgs(bound_method,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
767 format_spec,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
768 NULL);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
769 Py_DECREF(bound_method);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
770 } else {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
771 PyObject *self_as_str;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
772 PyObject *format_method;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
773
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
774 PyErr_Clear();
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
775 /* Per the PEP, convert to str (or unicode,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
776 depending on the type of the format
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
777 specifier). For new-style classes, this
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
778 logic is done by object.__format__(). */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
779 if (spec_is_unicode)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
780 self_as_str = PyObject_Unicode(obj);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
781 else
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
782 self_as_str = PyObject_Str(obj);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
783 if (self_as_str == NULL)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
784 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
785
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
786 /* Then call str.__format__ on that result */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
787 format_method = PyObject_GetAttr(self_as_str,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
788 str__format__);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
789 if (format_method == NULL) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
790 Py_DECREF(self_as_str);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
791 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
792 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
793 result = PyObject_CallFunctionObjArgs(format_method,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
794 format_spec,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
795 NULL);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
796 Py_DECREF(self_as_str);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
797 Py_DECREF(format_method);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
798 if (result == NULL)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
799 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
800 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
801 } else {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
802 /* Not an instance of a classic class, use the code
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
803 from py3k */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
804
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
805 /* Find the (unbound!) __format__ method (a borrowed
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
806 reference) */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
807 PyObject *method = _PyType_Lookup(Py_TYPE(obj),
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
808 str__format__);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
809 if (method == NULL) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
810 PyErr_Format(PyExc_TypeError,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
811 "Type %.100s doesn't define __format__",
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
812 Py_TYPE(obj)->tp_name);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
813 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
814 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
815 /* And call it, binding it to the value */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
816 result = PyObject_CallFunctionObjArgs(method, obj,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
817 format_spec, NULL);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
818 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
819
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
820 if (result == NULL)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
821 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
822
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
823 /* Check the result type, and make sure it's str or unicode */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
824 if (PyUnicode_Check(result))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
825 result_is_unicode = 1;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
826 else if (PyString_Check(result))
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
827 result_is_unicode = 0;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
828 else {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
829 PyErr_Format(PyExc_TypeError,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
830 "%.100s.__format__ must return string or "
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
831 "unicode, not %.100s", Py_TYPE(obj)->tp_name,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
832 Py_TYPE(result)->tp_name);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
833 Py_DECREF(result);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
834 result = NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
835 goto done;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
836 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
837
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
838 /* Convert to unicode, if needed. Required if spec is unicode
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
839 and result is str */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
840 if (spec_is_unicode && !result_is_unicode) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
841 PyObject *tmp = PyObject_Unicode(result);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
842 /* This logic works whether or not tmp is NULL */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
843 Py_DECREF(result);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
844 result = tmp;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
845 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
846
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
847 done:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
848 Py_XDECREF(empty);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
849 return result;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
850 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45359
diff changeset
851
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
852 /* Operations on numbers */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
853
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
854 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
855 PyNumber_Check(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
856 {
27937
8c211ccde2b4 Make PyNumber_Check() a bit more careful, since all sorts of things
Guido van Rossum <guido@python.org>
parents: 27830
diff changeset
857 return o && o->ob_type->tp_as_number &&
8c211ccde2b4 Make PyNumber_Check() a bit more careful, since all sorts of things
Guido van Rossum <guido@python.org>
parents: 27830
diff changeset
858 (o->ob_type->tp_as_number->nb_int ||
8c211ccde2b4 Make PyNumber_Check() a bit more careful, since all sorts of things
Guido van Rossum <guido@python.org>
parents: 27830
diff changeset
859 o->ob_type->tp_as_number->nb_float);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
860 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
861
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
862 /* Binary operators */
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
863
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
864 /* New style number protocol support */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
865
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
866 #define NB_SLOT(x) offsetof(PyNumberMethods, x)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
867 #define NB_BINOP(nb_methods, slot) \
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
868 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
869 #define NB_TERNOP(nb_methods, slot) \
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
870 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
871
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
872 /*
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
873 Calling scheme used for binary operations:
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
874
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
875 v w Action
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
876 -------------------------------------------------------------------
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
877 new new w.op(v,w)[*], v.op(v,w), w.op(v,w)
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
878 new old v.op(v,w), coerce(v,w), v.op(v,w)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
879 old new w.op(v,w), coerce(v,w), v.op(v,w)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
880 old old coerce(v,w), v.op(v,w)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
881
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
882 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
883 v->ob_type
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
884
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
885 Legend:
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
886 -------
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
887 * new == new style number
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
888 * old == old style number
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
889 * Action indicates the order in which operations are tried until either
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
890 a valid result is produced or an error occurs.
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
891
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
892 */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
893
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
894 static PyObject *
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
895 binary_op1(PyObject *v, PyObject *w, const int op_slot)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
896 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
897 PyObject *x;
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
898 binaryfunc slotv = NULL;
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
899 binaryfunc slotw = NULL;
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
900
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
901 if (v->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(v))
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
902 slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
903 if (w->ob_type != v->ob_type &&
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
904 w->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(w)) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
905 slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
906 if (slotw == slotv)
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
907 slotw = NULL;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
908 }
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
909 if (slotv) {
20203
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
910 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
911 x = slotw(v, w);
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
912 if (x != Py_NotImplemented)
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
913 return x;
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
914 Py_DECREF(x); /* can't do it */
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
915 slotw = NULL;
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
916 }
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
917 x = slotv(v, w);
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
918 if (x != Py_NotImplemented)
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
919 return x;
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
920 Py_DECREF(x); /* can't do it */
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
921 }
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
922 if (slotw) {
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
923 x = slotw(v, w);
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
924 if (x != Py_NotImplemented)
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
925 return x;
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
926 Py_DECREF(x); /* can't do it */
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
927 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
928 if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w)) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
929 int err = PyNumber_CoerceEx(&v, &w);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
930 if (err < 0) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
931 return NULL;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
932 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
933 if (err == 0) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
934 PyNumberMethods *mv = v->ob_type->tp_as_number;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
935 if (mv) {
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
936 binaryfunc slot;
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
937 slot = NB_BINOP(mv, op_slot);
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
938 if (slot) {
36244
e7880f4f53a4 Remove some shadowed variables
Neal Norwitz <nnorwitz@gmail.com>
parents: 36150
diff changeset
939 x = slot(v, w);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
940 Py_DECREF(v);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
941 Py_DECREF(w);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
942 return x;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
943 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
944 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
945 /* CoerceEx incremented the reference counts */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
946 Py_DECREF(v);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
947 Py_DECREF(w);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
948 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
949 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
950 Py_INCREF(Py_NotImplemented);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
951 return Py_NotImplemented;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
952 }
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
953
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
954 static PyObject *
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
955 binop_type_error(PyObject *v, PyObject *w, const char *op_name)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
956 {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
957 PyErr_Format(PyExc_TypeError,
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
958 "unsupported operand type(s) for %.100s: "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
959 "'%.100s' and '%.100s'",
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
960 op_name,
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
961 v->ob_type->tp_name,
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
962 w->ob_type->tp_name);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
963 return NULL;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
964 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
965
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
966 static PyObject *
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
967 binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
968 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
969 PyObject *result = binary_op1(v, w, op_slot);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
970 if (result == Py_NotImplemented) {
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
971 Py_DECREF(result);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
972 return binop_type_error(v, w, op_name);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 }
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
974 return result;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
975 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
976
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
977
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
978 /*
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
979 Calling scheme used for ternary operations:
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
980
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
981 *** In some cases, w.op is called before v.op; see binary_op1. ***
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
982
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
983 v w z Action
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
984 -------------------------------------------------------------------
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
985 new new new v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
986 new old new v.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
987 old new new w.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
988 old old new z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
989 new new old v.op(v,w,z), w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
990 new old old v.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
991 old new old w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
992 old old old coerce(v,w,z), v.op(v,w,z)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
993
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
994 Legend:
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
995 -------
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
996 * new == new style number
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
997 * old == old style number
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
998 * Action indicates the order in which operations are tried until either
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
999 a valid result is produced or an error occurs.
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1000 * coerce(v,w,z) actually does: coerce(v,w), coerce(v,z), coerce(w,z) and
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1001 only if z != Py_None; if z == Py_None, then it is treated as absent
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1002 variable and only coerce(v,w) is tried.
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1003
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1004 */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1005
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1006 static PyObject *
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1007 ternary_op(PyObject *v,
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1008 PyObject *w,
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1009 PyObject *z,
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1010 const int op_slot,
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1011 const char *op_name)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1012 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1013 PyNumberMethods *mv, *mw, *mz;
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1014 PyObject *x = NULL;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1015 ternaryfunc slotv = NULL;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1016 ternaryfunc slotw = NULL;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1017 ternaryfunc slotz = NULL;
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1018
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1019 mv = v->ob_type->tp_as_number;
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
1020 mw = w->ob_type->tp_as_number;
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1021 if (mv != NULL && NEW_STYLE_NUMBER(v))
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1022 slotv = NB_TERNOP(mv, op_slot);
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1023 if (w->ob_type != v->ob_type &&
26567
0601a90558fd Fix typo in abstract.c which caused __rpow__ to not be invoked.
Raymond Hettinger <python@rcn.com>
parents: 26386
diff changeset
1024 mw != NULL && NEW_STYLE_NUMBER(w)) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1025 slotw = NB_TERNOP(mw, op_slot);
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1026 if (slotw == slotv)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1027 slotw = NULL;
20169
abd6a08b2c32 It's a fact: for binary operators, *under certain circumstances*,
Guido van Rossum <guido@python.org>
parents: 19910
diff changeset
1028 }
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1029 if (slotv) {
20203
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1030 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1031 x = slotw(v, w, z);
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1032 if (x != Py_NotImplemented)
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1033 return x;
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1034 Py_DECREF(x); /* can't do it */
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1035 slotw = NULL;
9873c99c43f7 binary_op1(), ternary_op(): rearrange the code so that slotw is tested
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
1036 }
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1037 x = slotv(v, w, z);
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1038 if (x != Py_NotImplemented)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1039 return x;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1040 Py_DECREF(x); /* can't do it */
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1041 }
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1042 if (slotw) {
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1043 x = slotw(v, w, z);
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1044 if (x != Py_NotImplemented)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1045 return x;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1046 Py_DECREF(x); /* can't do it */
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1047 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1048 mz = z->ob_type->tp_as_number;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1049 if (mz != NULL && NEW_STYLE_NUMBER(z)) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1050 slotz = NB_TERNOP(mz, op_slot);
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1051 if (slotz == slotv || slotz == slotw)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1052 slotz = NULL;
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1053 if (slotz) {
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1054 x = slotz(v, w, z);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1055 if (x != Py_NotImplemented)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1056 return x;
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1057 Py_DECREF(x); /* can't do it */
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1058 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1059 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1060
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1061 if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w) ||
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1062 (z != Py_None && !NEW_STYLE_NUMBER(z))) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1063 /* we have an old style operand, coerce */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1064 PyObject *v1, *z1, *w2, *z2;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1065 int c;
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1066
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1067 c = PyNumber_Coerce(&v, &w);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1068 if (c != 0)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1069 goto error3;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1070
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1071 /* Special case: if the third argument is None, it is
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1072 treated as absent argument and not coerced. */
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1073 if (z == Py_None) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1074 if (v->ob_type->tp_as_number) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1075 slotz = NB_TERNOP(v->ob_type->tp_as_number,
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1076 op_slot);
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1077 if (slotz)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1078 x = slotz(v, w, z);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1079 else
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1080 c = -1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1081 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1082 else
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1083 c = -1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1084 goto error2;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1085 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1086 v1 = v;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1087 z1 = z;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1088 c = PyNumber_Coerce(&v1, &z1);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1089 if (c != 0)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1090 goto error2;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1091 w2 = w;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1092 z2 = z1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1093 c = PyNumber_Coerce(&w2, &z2);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1094 if (c != 0)
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1095 goto error1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1096
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1097 if (v1->ob_type->tp_as_number != NULL) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1098 slotv = NB_TERNOP(v1->ob_type->tp_as_number,
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1099 op_slot);
20172
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1100 if (slotv)
de3e851818a4 The changes to ternary_op could cause a core dump. Fix this, and
Guido van Rossum <guido@python.org>
parents: 20169
diff changeset
1101 x = slotv(v1, w2, z2);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1102 else
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1103 c = -1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1104 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1105 else
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1106 c = -1;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1107
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1108 Py_DECREF(w2);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1109 Py_DECREF(z2);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1110 error1:
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1111 Py_DECREF(v1);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1112 Py_DECREF(z1);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1113 error2:
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1114 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1115 Py_DECREF(w);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1116 error3:
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1117 if (c >= 0)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1118 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1119 }
20604
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1120
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1121 if (z == Py_None)
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1122 PyErr_Format(
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1123 PyExc_TypeError,
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1124 "unsupported operand type(s) for ** or pow(): "
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1125 "'%.100s' and '%.100s'",
20604
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1126 v->ob_type->tp_name,
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1127 w->ob_type->tp_name);
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1128 else
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1129 PyErr_Format(
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1130 PyExc_TypeError,
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1131 "unsupported operand type(s) for pow(): "
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1132 "'%.100s', '%.100s', '%.100s'",
20604
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1133 v->ob_type->tp_name,
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1134 w->ob_type->tp_name,
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20507
diff changeset
1135 z->ob_type->tp_name);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1136 return NULL;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1137 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1138
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1139 #define BINARY_FUNC(func, op, op_name) \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1140 PyObject * \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1141 func(PyObject *v, PyObject *w) { \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1142 return binary_op(v, w, NB_SLOT(op), op_name); \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1143 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1144
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1145 BINARY_FUNC(PyNumber_Or, nb_or, "|")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1146 BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1147 BINARY_FUNC(PyNumber_And, nb_and, "&")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1148 BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1149 BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1150 BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1151 BINARY_FUNC(PyNumber_Divide, nb_divide, "/")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1152 BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1153
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1154 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1155 PyNumber_Add(PyObject *v, PyObject *w)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1156 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1157 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1158 if (result == Py_NotImplemented) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1159 PySequenceMethods *m = v->ob_type->tp_as_sequence;
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1160 Py_DECREF(result);
22194
fc6dc2ea25cf Fix leak of NotImplemented in previous checkin to PyNumber_Add().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22193
diff changeset
1161 if (m && m->sq_concat) {
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1162 return (*m->sq_concat)(v, w);
22194
fc6dc2ea25cf Fix leak of NotImplemented in previous checkin to PyNumber_Add().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22193
diff changeset
1163 }
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1164 result = binop_type_error(v, w, "+");
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1165 }
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1166 return result;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1167 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1168
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1169 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1170 sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1171 {
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1172 Py_ssize_t count;
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1173 if (PyIndex_Check(n)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1174 count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1175 if (count == -1 && PyErr_Occurred())
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1176 return NULL;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1177 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1178 else {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1179 return type_error("can't multiply sequence by "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1180 "non-int of type '%.200s'", n);
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1181 }
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1182 return (*repeatfunc)(seq, count);
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1183 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1184
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1185 PyObject *
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1186 PyNumber_Multiply(PyObject *v, PyObject *w)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1187 {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1188 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1189 if (result == Py_NotImplemented) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1190 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1191 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
26987
92e572e3ca9f Merge to trunk from release branch:
Guido van Rossum <guido@python.org>
parents: 26865
diff changeset
1192 Py_DECREF(result);
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1193 if (mv && mv->sq_repeat) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1194 return sequence_repeat(mv->sq_repeat, v, w);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1195 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1196 else if (mw && mw->sq_repeat) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1197 return sequence_repeat(mw->sq_repeat, w, v);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1198 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1199 result = binop_type_error(v, w, "*");
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1200 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1201 return result;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1202 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1203
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1204 PyObject *
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1205 PyNumber_FloorDivide(PyObject *v, PyObject *w)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1206 {
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1207 /* XXX tp_flags test */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1208 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1209 }
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1210
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1211 PyObject *
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1212 PyNumber_TrueDivide(PyObject *v, PyObject *w)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1213 {
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1214 /* XXX tp_flags test */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1215 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1216 }
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1217
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1218 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1219 PyNumber_Remainder(PyObject *v, PyObject *w)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1220 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1221 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1222 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1223
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1224 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1225 PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1226 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1227 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1228 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1230 /* Binary in-place operators */
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1231
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1232 /* The in-place operators are defined to fall back to the 'normal',
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1233 non in-place operations, if the in-place methods are not in place.
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1234
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1235 - If the left hand object has the appropriate struct members, and
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1236 they are filled, call the appropriate function and return the
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1237 result. No coercion is done on the arguments; the left-hand object
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1238 is the one the operation is performed on, and it's up to the
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1239 function to deal with the right-hand object.
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1240
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1241 - Otherwise, in-place modification is not supported. Handle it exactly as
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1242 a non in-place operation of the same kind.
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1243
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1244 */
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1245
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1246 #define HASINPLACE(t) \
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1247 PyType_HasFeature((t)->ob_type, Py_TPFLAGS_HAVE_INPLACEOPS)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1248
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1249 static PyObject *
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1250 binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1251 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1252 PyNumberMethods *mv = v->ob_type->tp_as_number;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1253 if (mv != NULL && HASINPLACE(v)) {
26386
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1254 binaryfunc slot = NB_BINOP(mv, iop_slot);
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1255 if (slot) {
df719736e151 Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26383
diff changeset
1256 PyObject *x = (slot)(v, w);
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1257 if (x != Py_NotImplemented) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1258 return x;
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1259 }
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1260 Py_DECREF(x);
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1261 }
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1262 }
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1263 return binary_op1(v, w, op_slot);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1264 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1265
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1266 static PyObject *
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1267 binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1268 const char *op_name)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1269 {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1270 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1271 if (result == Py_NotImplemented) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1272 Py_DECREF(result);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1273 return binop_type_error(v, w, op_name);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1274 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1275 return result;
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1276 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1277
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1278 #define INPLACE_BINOP(func, iop, op, op_name) \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1279 PyObject * \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1280 func(PyObject *v, PyObject *w) { \
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1281 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1282 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1283
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1284 INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1285 INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1286 INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1287 INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1288 INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1289 INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1290 INPLACE_BINOP(PyNumber_InPlaceDivide, nb_inplace_divide, nb_divide, "/=")
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1291
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1292 PyObject *
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1293 PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1294 {
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1295 /* XXX tp_flags test */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1296 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1297 NB_SLOT(nb_floor_divide), "//=");
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1298 }
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1299
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1300 PyObject *
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1301 PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1302 {
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1303 /* XXX tp_flags test */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1304 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1305 NB_SLOT(nb_true_divide), "/=");
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1306 }
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1307
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1308 PyObject *
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1309 PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1310 {
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1311 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1312 NB_SLOT(nb_add));
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1313 if (result == Py_NotImplemented) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1314 PySequenceMethods *m = v->ob_type->tp_as_sequence;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1315 Py_DECREF(result);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1316 if (m != NULL) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1317 binaryfunc f = NULL;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1318 if (HASINPLACE(v))
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1319 f = m->sq_inplace_concat;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1320 if (f == NULL)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1321 f = m->sq_concat;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1322 if (f != NULL)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1323 return (*f)(v, w);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1324 }
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1325 result = binop_type_error(v, w, "+=");
14374
8df166ca09b2 Rewritten some pieces of PyNumber_InPlaceAdd() for clarity.
Guido van Rossum <guido@python.org>
parents: 14343
diff changeset
1326 }
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1327 return result;
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1328 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1329
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1330 PyObject *
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1331 PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1332 {
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1333 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1334 NB_SLOT(nb_multiply));
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1335 if (result == Py_NotImplemented) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1336 ssizeargfunc f = NULL;
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1337 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1338 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1339 Py_DECREF(result);
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1340 if (mv != NULL) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1341 if (HASINPLACE(v))
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1342 f = mv->sq_inplace_repeat;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1343 if (f == NULL)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1344 f = mv->sq_repeat;
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1345 if (f != NULL)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1346 return sequence_repeat(f, v, w);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1347 }
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1348 else if (mw != NULL) {
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1349 /* Note that the right hand operand should not be
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1350 * mutated in this case so sq_inplace_repeat is not
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1351 * used. */
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1352 if (mw->sq_repeat)
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1353 return sequence_repeat(mw->sq_repeat, w, v);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1354 }
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1355 result = binop_type_error(v, w, "*=");
14377
dd4766aa2fd2 Cosmetic cleanup by Vladimir.
Thomas Wouters <thomas@python.org>
parents: 14374
diff changeset
1356 }
26865
aaca8a3b23cd Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26622
diff changeset
1357 return result;
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1358 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1359
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1360 PyObject *
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1361 PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1362 {
26383
e7486f3ef759 Remove special handling of str and unicode in PyNumber_InPlaceRemainder. They
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26271
diff changeset
1363 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
e7486f3ef759 Remove special handling of str and unicode in PyNumber_InPlaceRemainder. They
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26271
diff changeset
1364 NB_SLOT(nb_remainder), "%=");
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1365 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1366
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1367 PyObject *
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1368 PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1369 {
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1370 if (HASINPLACE(v) && v->ob_type->tp_as_number &&
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1371 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1372 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1373 }
15721
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1374 else {
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1375 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
ead8b5b994ad Massive changes as per PEP 208. Read it for details.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15029
diff changeset
1376 }
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1377 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1378
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1379
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1380 /* Unary operators and functions */
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1381
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1382 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1383 PyNumber_Negative(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1384 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1385 PyNumberMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1386
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1387 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1388 return null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1389 m = o->ob_type->tp_as_number;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1390 if (m && m->nb_negative)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1391 return (*m->nb_negative)(o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1392
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1393 return type_error("bad operand type for unary -: '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1394 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1395
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1396 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1397 PyNumber_Positive(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1398 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1399 PyNumberMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1401 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1402 return null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1403 m = o->ob_type->tp_as_number;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1404 if (m && m->nb_positive)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1405 return (*m->nb_positive)(o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1406
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1407 return type_error("bad operand type for unary +: '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1408 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1409
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1410 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1411 PyNumber_Invert(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1412 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1413 PyNumberMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1414
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1415 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1416 return null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1417 m = o->ob_type->tp_as_number;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1418 if (m && m->nb_invert)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1419 return (*m->nb_invert)(o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1420
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1421 return type_error("bad operand type for unary ~: '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1422 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1423
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1424 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1425 PyNumber_Absolute(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1426 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1427 PyNumberMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1428
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1429 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1430 return null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1431 m = o->ob_type->tp_as_number;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1432 if (m && m->nb_absolute)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1433 return m->nb_absolute(o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1434
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1435 return type_error("bad operand type for abs(): '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1436 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1437
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1438 /* Add a check for embedded NULL-bytes in the argument. */
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1439 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1440 int_from_string(const char *s, Py_ssize_t len)
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1441 {
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1442 char *end;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1443 PyObject *x;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1444
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1445 x = PyInt_FromString((char*)s, &end, 10);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1446 if (x == NULL)
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1447 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1448 if (end != s + len) {
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1449 PyErr_SetString(PyExc_ValueError,
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1450 "null byte in argument for int()");
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1451 Py_DECREF(x);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1452 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1453 }
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1454 return x;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1455 }
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1456
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1457 /* Return a Python Int or Long from the object item
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1458 Raise TypeError if the result is not an int-or-long
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1459 or if the object cannot be interpreted as an index.
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1460 */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1461 PyObject *
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1462 PyNumber_Index(PyObject *item)
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1463 {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1464 PyObject *result = NULL;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1465 if (item == NULL)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1466 return null_error();
39507
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1467 if (PyInt_Check(item) || PyLong_Check(item)) {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1468 Py_INCREF(item);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1469 return item;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1470 }
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1471 if (PyIndex_Check(item)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1472 result = item->ob_type->tp_as_number->nb_index(item);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1473 if (result &&
39507
4f90c1d530d9 Subclasses of int/long are allowed to define an __index__.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1474 !PyInt_Check(result) && !PyLong_Check(result)) {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1475 PyErr_Format(PyExc_TypeError,
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1476 "__index__ returned non-(int,long) " \
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1477 "(type %.200s)",
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1478 result->ob_type->tp_name);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1479 Py_DECREF(result);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1480 return NULL;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1481 }
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1482 }
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1483 else {
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37165
diff changeset
1484 PyErr_Format(PyExc_TypeError,
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37165
diff changeset
1485 "'%.200s' object cannot be interpreted "
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37165
diff changeset
1486 "as an index", item->ob_type->tp_name);
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1487 }
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1488 return result;
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1489 }
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36834
diff changeset
1490
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1491 /* Return an error on Overflow only if err is not NULL*/
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1492
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1493 Py_ssize_t
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1494 PyNumber_AsSsize_t(PyObject *item, PyObject *err)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1495 {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1496 Py_ssize_t result;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1497 PyObject *runerr;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1498 PyObject *value = PyNumber_Index(item);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1499 if (value == NULL)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1500 return -1;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1501
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1502 /* We're done if PyInt_AsSsize_t() returns without error. */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1503 result = PyInt_AsSsize_t(value);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1504 if (result != -1 || !(runerr = PyErr_Occurred()))
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1505 goto finish;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1506
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1507 /* Error handling code -- only manage OverflowError differently */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1508 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1509 goto finish;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1510
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1511 PyErr_Clear();
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1512 /* If no error-handling desired then the default clipping
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1513 is sufficient.
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1514 */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1515 if (!err) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1516 assert(PyLong_Check(value));
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1517 /* Whether or not it is less than or equal to
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1518 zero is determined by the sign of ob_size
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1519 */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1520 if (_PyLong_Sign(value) < 0)
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1521 result = PY_SSIZE_T_MIN;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1522 else
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1523 result = PY_SSIZE_T_MAX;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1524 }
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1525 else {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1526 /* Otherwise replace the error with caller's error object. */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1527 PyErr_Format(err,
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1528 "cannot fit '%.200s' into an index-sized integer",
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1529 item->ob_type->tp_name);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1530 }
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1531
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1532 finish:
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1533 Py_DECREF(value);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1534 return result;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1535 }
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1536
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39394
diff changeset
1537
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1538 PyObject *
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1539 _PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format)
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1540 {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1541 const char *type_name;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1542 static PyObject *int_name = NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1543 if (int_name == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1544 int_name = PyString_InternFromString("__int__");
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1545 if (int_name == NULL)
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1546 return NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1547 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1548
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1549 if (integral && (!PyInt_Check(integral) &&
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1550 !PyLong_Check(integral))) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1551 /* Don't go through tp_as_number->nb_int to avoid
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1552 hitting the classic class fallback to __trunc__. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1553 PyObject *int_func = PyObject_GetAttr(integral, int_name);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1554 if (int_func == NULL) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1555 PyErr_Clear(); /* Raise a different error. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1556 goto non_integral_error;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1557 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1558 Py_DECREF(integral);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1559 integral = PyEval_CallObject(int_func, NULL);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1560 Py_DECREF(int_func);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1561 if (integral && (!PyInt_Check(integral) &&
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1562 !PyLong_Check(integral))) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1563 goto non_integral_error;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1564 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1565 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1566 return integral;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1567
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1568 non_integral_error:
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1569 if (PyInstance_Check(integral)) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1570 type_name = PyString_AS_STRING(((PyInstanceObject *)integral)
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1571 ->in_class->cl_name);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1572 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1573 else {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1574 type_name = integral->ob_type->tp_name;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1575 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1576 PyErr_Format(PyExc_TypeError, error_format, type_name);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1577 Py_DECREF(integral);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1578 return NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1579 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1580
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1581
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1582 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1583 PyNumber_Int(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1584 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1585 PyNumberMethods *m;
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1586 static PyObject *trunc_name = NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1587 PyObject *trunc_func;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1588 const char *buffer;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1589 Py_ssize_t buffer_len;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1590
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1591 if (trunc_name == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1592 trunc_name = PyString_InternFromString("__trunc__");
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1593 if (trunc_name == NULL)
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1594 return NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1595 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1596
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1597 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1598 return null_error();
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
1599 if (PyInt_CheckExact(o)) {
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1600 Py_INCREF(o);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1601 return o;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1602 }
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1603 m = o->ob_type->tp_as_number;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1604 if (m && m->nb_int) { /* This should include subclasses of int */
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1605 /* Classic classes always take this branch. */
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1606 PyObject *res = m->nb_int(o);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1607 if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1608 PyErr_Format(PyExc_TypeError,
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1609 "__int__ returned non-int (type %.200s)",
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1610 res->ob_type->tp_name);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1611 Py_DECREF(res);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1612 return NULL;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1613 }
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1614 return res;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1615 }
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1616 if (PyInt_Check(o)) { /* A int subclass without nb_int */
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
1617 PyIntObject *io = (PyIntObject*)o;
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
1618 return PyInt_FromLong(io->ob_ival);
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
1619 }
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1620 trunc_func = PyObject_GetAttr(o, trunc_name);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1621 if (trunc_func) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1622 PyObject *truncated = PyEval_CallObject(trunc_func, NULL);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1623 Py_DECREF(trunc_func);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1624 /* __trunc__ is specified to return an Integral type, but
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1625 int() needs to return an int. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1626 return _PyNumber_ConvertIntegralToInt(
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1627 truncated,
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1628 "__trunc__ returned non-Integral (type %.200s)");
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1629 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1630 PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1631
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1632 if (PyString_Check(o))
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1633 return int_from_string(PyString_AS_STRING(o),
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1634 PyString_GET_SIZE(o));
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1635 #ifdef Py_USING_UNICODE
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1636 if (PyUnicode_Check(o))
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1637 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1638 PyUnicode_GET_SIZE(o),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1639 10);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1640 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1641 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1642 return int_from_string((char*)buffer, buffer_len);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1643
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1644 return type_error("int() argument must be a string or a "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1645 "number, not '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1646 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1647
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1648 /* Add a check for embedded NULL-bytes in the argument. */
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1649 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1650 long_from_string(const char *s, Py_ssize_t len)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1651 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1652 char *end;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1653 PyObject *x;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1654
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1655 x = PyLong_FromString((char*)s, &end, 10);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1656 if (x == NULL)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1657 return NULL;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1658 if (end != s + len) {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1659 PyErr_SetString(PyExc_ValueError,
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1660 "null byte in argument for long()");
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1661 Py_DECREF(x);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1662 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1663 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1664 return x;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1665 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1666
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1667 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1668 PyNumber_Long(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1669 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1670 PyNumberMethods *m;
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1671 static PyObject *trunc_name = NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1672 PyObject *trunc_func;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1673 const char *buffer;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1674 Py_ssize_t buffer_len;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1675
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1676 if (trunc_name == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1677 trunc_name = PyString_InternFromString("__trunc__");
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1678 if (trunc_name == NULL)
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1679 return NULL;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1680 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1681
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1682 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1683 return null_error();
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1684 m = o->ob_type->tp_as_number;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1685 if (m && m->nb_long) { /* This should include subclasses of long */
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1686 /* Classic classes always take this branch. */
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1687 PyObject *res = m->nb_long(o);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1688 if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1689 PyErr_Format(PyExc_TypeError,
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1690 "__long__ returned non-long (type %.200s)",
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1691 res->ob_type->tp_name);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1692 Py_DECREF(res);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1693 return NULL;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1694 }
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1695 return res;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1696 }
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1697 if (PyLong_Check(o)) /* A long subclass without nb_long */
22108
354cde7f90a1 Revert the last odd change to PyNumber_Long: the problem it was trying
Tim Peters <tim.peters@gmail.com>
parents: 22106
diff changeset
1698 return _PyLong_Copy((PyLongObject *)o);
45142
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1699 trunc_func = PyObject_GetAttr(o, trunc_name);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1700 if (trunc_func) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1701 PyObject *truncated = PyEval_CallObject(trunc_func, NULL);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1702 PyObject *int_instance;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1703 Py_DECREF(trunc_func);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1704 /* __trunc__ is specified to return an Integral type,
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1705 but long() needs to return a long. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1706 int_instance = _PyNumber_ConvertIntegralToInt(
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1707 truncated,
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1708 "__trunc__ returned non-Integral (type %.200s)");
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1709 if (int_instance && PyInt_Check(int_instance)) {
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1710 /* Make sure that long() returns a long instance. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1711 long value = PyInt_AS_LONG(int_instance);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1712 Py_DECREF(int_instance);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1713 return PyLong_FromLong(value);
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1714 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1715 return int_instance;
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1716 }
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1717 PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */
f03955faffb4 Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45028
diff changeset
1718
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1719 if (PyString_Check(o))
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
1720 /* need to do extra error checking that PyLong_FromString()
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1721 * doesn't do. In particular long('9.5') must raise an
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1722 * exception, not truncate the float.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 9834
diff changeset
1723 */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1724 return long_from_string(PyString_AS_STRING(o),
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
1725 PyString_GET_SIZE(o));
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1726 #ifdef Py_USING_UNICODE
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1727 if (PyUnicode_Check(o))
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1728 /* The above check is done in PyLong_FromUnicode(). */
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1729 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1730 PyUnicode_GET_SIZE(o),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1731 10);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1732 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1733 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1734 return long_from_string(buffer, buffer_len);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1735
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1736 return type_error("long() argument must be a string or a "
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1737 "number, not '%.200s'", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1738 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1739
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1740 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1741 PyNumber_Float(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1742 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1743 PyNumberMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1744
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1745 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1746 return null_error();
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1747 m = o->ob_type->tp_as_number;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1748 if (m && m->nb_float) { /* This should include subclasses of float */
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1749 PyObject *res = m->nb_float(o);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1750 if (res && !PyFloat_Check(res)) {
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1751 PyErr_Format(PyExc_TypeError,
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1752 "__float__ returned non-float (type %.200s)",
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1753 res->ob_type->tp_name);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1754 Py_DECREF(res);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1755 return NULL;
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1756 }
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1757 return res;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1758 }
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34182
diff changeset
1759 if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
19839
10f5927dbf05 More for SF bug [#460020] bug or feature: unicode() and subclasses
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1760 PyFloatObject *po = (PyFloatObject *)o;
10f5927dbf05 More for SF bug [#460020] bug or feature: unicode() and subclasses
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1761 return PyFloat_FromDouble(po->ob_fval);
10f5927dbf05 More for SF bug [#460020] bug or feature: unicode() and subclasses
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1762 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11943
diff changeset
1763 return PyFloat_FromString(o, NULL);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1764 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1765
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1766 PyObject *
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1767 PyNumber_ToBase(PyObject *n, int base)
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1768 {
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1769 PyObject *res = NULL;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1770 PyObject *index = PyNumber_Index(n);
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1771
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1772 if (!index)
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1773 return NULL;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1774 if (PyLong_Check(index))
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1775 res = _PyLong_Format(index, base, 0, 1);
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1776 else if (PyInt_Check(index))
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1777 res = _PyInt_Format((PyIntObject*)index, base, 1);
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1778 else
45359
3a55c0fd822f In PyNumber_ToBase, changed from an assert to returning an error when PyObject_Index() returns something other than an int or long. It should never be possible to trigger this, as PyObject_Index checks to make sure it returns an int or long.
Eric Smith <eric@trueblade.com>
parents: 45356
diff changeset
1779 /* It should not be possible to get here, as
3a55c0fd822f In PyNumber_ToBase, changed from an assert to returning an error when PyObject_Index() returns something other than an int or long. It should never be possible to trigger this, as PyObject_Index checks to make sure it returns an int or long.
Eric Smith <eric@trueblade.com>
parents: 45356
diff changeset
1780 PyNumber_Index already has a check for the same
3a55c0fd822f In PyNumber_ToBase, changed from an assert to returning an error when PyObject_Index() returns something other than an int or long. It should never be possible to trigger this, as PyObject_Index checks to make sure it returns an int or long.
Eric Smith <eric@trueblade.com>
parents: 45356
diff changeset
1781 condition */
3a55c0fd822f In PyNumber_ToBase, changed from an assert to returning an error when PyObject_Index() returns something other than an int or long. It should never be possible to trigger this, as PyObject_Index checks to make sure it returns an int or long.
Eric Smith <eric@trueblade.com>
parents: 45356
diff changeset
1782 PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not "
3a55c0fd822f In PyNumber_ToBase, changed from an assert to returning an error when PyObject_Index() returns something other than an int or long. It should never be possible to trigger this, as PyObject_Index checks to make sure it returns an int or long.
Eric Smith <eric@trueblade.com>
parents: 45356
diff changeset
1783 "int or long");
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1784 Py_DECREF(index);
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1785 return res;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1786 }
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1787
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45142
diff changeset
1788
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1789 /* Operations on sequences */
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1790
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1791 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1792 PySequence_Check(PyObject *s)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1793 {
33521
55eca7cd3531 SF bug #1030557: PyMapping_Check crashes when argument is NULL
Raymond Hettinger <python@rcn.com>
parents: 32759
diff changeset
1794 if (s && PyInstance_Check(s))
31870
c7f9fca946d1 Improve accuracy of sequence and mapping checks.
Raymond Hettinger <python@rcn.com>
parents: 31734
diff changeset
1795 return PyObject_HasAttrString(s, "__getitem__");
40764
c994c245d9ab Silence compiler warning
Raymond Hettinger <python@rcn.com>
parents: 40762
diff changeset
1796 if (PyObject_IsInstance(s, (PyObject *)&PyDict_Type))
40762
ffaaae02c662 Bug #1575169: operator.isSequenceType() now returns False for subclasses of dict.
Raymond Hettinger <python@rcn.com>
parents: 39995
diff changeset
1797 return 0;
19804
9031aab27eae PySequence_Check(), PyMapping_Check(): only return true if the
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
1798 return s != NULL && s->ob_type->tp_as_sequence &&
9031aab27eae PySequence_Check(), PyMapping_Check(): only return true if the
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
1799 s->ob_type->tp_as_sequence->sq_item != NULL;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1800 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1801
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1802 Py_ssize_t
13575
7c119f050290 change abstract size functions PySequence_Size &c.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13451
diff changeset
1803 PySequence_Size(PyObject *s)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1804 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1805 PySequenceMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1806
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1807 if (s == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1808 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1809 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1810 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1811
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1812 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1813 if (m && m->sq_length)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1814 return m->sq_length(s);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1815
39394
f5a7f4dd93e0 Remove "non-mapping" and "non-sequence" from TypeErrors raised by
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
1816 type_error("object of type '%.200s' has no len()", s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1817 return -1;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1818 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1819
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1820 #undef PySequence_Length
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1821 Py_ssize_t
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1822 PySequence_Length(PyObject *s)
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1823 {
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1824 return PySequence_Size(s);
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1825 }
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1826 #define PySequence_Length PySequence_Size
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
1827
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1828 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1829 PySequence_Concat(PyObject *s, PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1830 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1831 PySequenceMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1832
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1833 if (s == NULL || o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1834 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1835
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1836 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1837 if (m && m->sq_concat)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1838 return m->sq_concat(s, o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1839
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1840 /* Instances of user classes defining an __add__() method only
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1841 have an nb_add slot, not an sq_concat slot. So we fall back
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1842 to nb_add if both arguments appear to be sequences. */
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1843 if (PySequence_Check(s) && PySequence_Check(o)) {
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1844 PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1845 if (result != Py_NotImplemented)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1846 return result;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1847 Py_DECREF(result);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1848 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1849 return type_error("'%.200s' object can't be concatenated", s);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1850 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1851
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1852 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1853 PySequence_Repeat(PyObject *o, Py_ssize_t count)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1854 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1855 PySequenceMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1856
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1857 if (o == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1858 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1859
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1860 m = o->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1861 if (m && m->sq_repeat)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1862 return m->sq_repeat(o, count);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1863
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1864 /* Instances of user classes defining a __mul__() method only
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1865 have an nb_multiply slot, not an sq_repeat slot. so we fall back
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1866 to nb_multiply if o appears to be a sequence. */
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1867 if (PySequence_Check(o)) {
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1868 PyObject *n, *result;
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1869 n = PyInt_FromSsize_t(count);
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1870 if (n == NULL)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1871 return NULL;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1872 result = binary_op1(o, n, NB_SLOT(nb_multiply));
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1873 Py_DECREF(n);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1874 if (result != Py_NotImplemented)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1875 return result;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1876 Py_DECREF(result);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1877 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1878 return type_error("'%.200s' object can't be repeated", o);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1879 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1880
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1881 PyObject *
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1882 PySequence_InPlaceConcat(PyObject *s, PyObject *o)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1883 {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1884 PySequenceMethods *m;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1885
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1886 if (s == NULL || o == NULL)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1887 return null_error();
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1888
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1889 m = s->ob_type->tp_as_sequence;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1890 if (m && HASINPLACE(s) && m->sq_inplace_concat)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1891 return m->sq_inplace_concat(s, o);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1892 if (m && m->sq_concat)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1893 return m->sq_concat(s, o);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1894
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1895 if (PySequence_Check(s) && PySequence_Check(o)) {
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1896 PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1897 NB_SLOT(nb_add));
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1898 if (result != Py_NotImplemented)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1899 return result;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1900 Py_DECREF(result);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1901 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1902 return type_error("'%.200s' object can't be concatenated", s);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1903 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1904
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1905 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1906 PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1907 {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1908 PySequenceMethods *m;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1909
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1910 if (o == NULL)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1911 return null_error();
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1912
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1913 m = o->ob_type->tp_as_sequence;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1914 if (m && HASINPLACE(o) && m->sq_inplace_repeat)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1915 return m->sq_inplace_repeat(o, count);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1916 if (m && m->sq_repeat)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1917 return m->sq_repeat(o, count);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1918
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1919 if (PySequence_Check(o)) {
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1920 PyObject *n, *result;
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1921 n = PyInt_FromSsize_t(count);
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1922 if (n == NULL)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1923 return NULL;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1924 result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1925 NB_SLOT(nb_multiply));
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1926 Py_DECREF(n);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1927 if (result != Py_NotImplemented)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1928 return result;
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1929 Py_DECREF(result);
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35636
diff changeset
1930 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1931 return type_error("'%.200s' object can't be repeated", o);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1932 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1933
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14146
diff changeset
1934 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1935 PySequence_GetItem(PyObject *s, Py_ssize_t i)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1936 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1937 PySequenceMethods *m;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1938
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1939 if (s == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1940 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1941
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1942 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1943 if (m && m->sq_item) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1944 if (i < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1945 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1946 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1947 if (l < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1948 return NULL;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1949 i += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1950 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1951 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1952 return m->sq_item(s, i);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1953 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1954
47910
26799861d318 Correct grammar.
Skip Montanaro <skip@pobox.com>
parents: 47527
diff changeset
1955 return type_error("'%.200s' object does not support indexing", s);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1956 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1957
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1958 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1959 PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1960 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1961 PySequenceMethods *m;
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1962 PyMappingMethods *mp;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1963
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1964 if (!s) return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1965
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1966 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1967 if (m && m->sq_slice) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1968 if (i1 < 0 || i2 < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1969 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1970 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1971 if (l < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1972 return NULL;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1973 if (i1 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1974 i1 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1975 if (i2 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1976 i2 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1977 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1978 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1979 return m->sq_slice(s, i1, i2);
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1980 } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_subscript) {
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1981 PyObject *res;
37165
45dd938b7feb Stop duplicating code and handle slice indices consistently and correctly
Neal Norwitz <nnorwitz@gmail.com>
parents: 37087
diff changeset
1982 PyObject *slice = _PySlice_FromIndices(i1, i2);
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1983 if (!slice)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1984 return NULL;
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1985 res = mp->mp_subscript(s, slice);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1986 Py_DECREF(slice);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
1987 return res;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1988 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1989
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
1990 return type_error("'%.200s' object is unsliceable", s);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1991 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1992
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1993 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1994 PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1995 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1996 PySequenceMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1997
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1998 if (s == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
1999 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2000 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2001 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2002
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2003 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2004 if (m && m->sq_ass_item) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2005 if (i < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2006 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2007 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2008 if (l < 0)
8452
84d1b7cf815a Fix a whole bunch of error return NULL that should be return -1.
Guido van Rossum <guido@python.org>
parents: 8437
diff changeset
2009 return -1;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2010 i += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2011 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2012 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2013 return m->sq_ass_item(s, i, o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2014 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2015
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2016 type_error("'%.200s' object does not support item assignment", s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2017 return -1;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2018 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2019
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2020 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2021 PySequence_DelItem(PyObject *s, Py_ssize_t i)
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2022 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2023 PySequenceMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2024
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2025 if (s == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2026 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2027 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2028 }
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2029
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2030 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2031 if (m && m->sq_ass_item) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2032 if (i < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2033 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2034 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2035 if (l < 0)
8452
84d1b7cf815a Fix a whole bunch of error return NULL that should be return -1.
Guido van Rossum <guido@python.org>
parents: 8437
diff changeset
2036 return -1;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2037 i += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2038 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2039 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2040 return m->sq_ass_item(s, i, (PyObject *)NULL);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2041 }
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2042
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2043 type_error("'%.200s' object doesn't support item deletion", s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2044 return -1;
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2045 }
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2046
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2047 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2048 PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2049 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2050 PySequenceMethods *m;
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2051 PyMappingMethods *mp;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2052
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2053 if (s == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2054 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2055 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2056 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2057
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2058 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2059 if (m && m->sq_ass_slice) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2060 if (i1 < 0 || i2 < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2061 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2062 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2063 if (l < 0)
8452
84d1b7cf815a Fix a whole bunch of error return NULL that should be return -1.
Guido van Rossum <guido@python.org>
parents: 8437
diff changeset
2064 return -1;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2065 if (i1 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2066 i1 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2067 if (i2 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2068 i2 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2069 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2070 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2071 return m->sq_ass_slice(s, i1, i2, o);
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2072 } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) {
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2073 int res;
37165
45dd938b7feb Stop duplicating code and handle slice indices consistently and correctly
Neal Norwitz <nnorwitz@gmail.com>
parents: 37087
diff changeset
2074 PyObject *slice = _PySlice_FromIndices(i1, i2);
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2075 if (!slice)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2076 return -1;
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2077 res = mp->mp_ass_subscript(s, slice, o);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2078 Py_DECREF(slice);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2079 return res;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2080 }
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13830
diff changeset
2081
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2082 type_error("'%.200s' object doesn't support slice assignment", s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2083 return -1;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2084 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2085
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2086 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2087 PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2088 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2089 PySequenceMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2090
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2091 if (s == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2092 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2093 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2094 }
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2095
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2096 m = s->ob_type->tp_as_sequence;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2097 if (m && m->sq_ass_slice) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2098 if (i1 < 0 || i2 < 0) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2099 if (m->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2100 Py_ssize_t l = (*m->sq_length)(s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2101 if (l < 0)
8452
84d1b7cf815a Fix a whole bunch of error return NULL that should be return -1.
Guido van Rossum <guido@python.org>
parents: 8437
diff changeset
2102 return -1;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2103 if (i1 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2104 i1 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2105 if (i2 < 0)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2106 i2 += l;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2107 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2108 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2109 return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2110 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2111 type_error("'%.200s' object doesn't support slice deletion", s);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2112 return -1;
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2113 }
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
2114
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2115 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2116 PySequence_Tuple(PyObject *v)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2117 {
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2118 PyObject *it; /* iter(v) */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2119 Py_ssize_t n; /* guess for result tuple size */
51282
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
2120 PyObject *result = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2121 Py_ssize_t j;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2122
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2123 if (v == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2124 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2125
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2126 /* Special-case the common tuple and list cases, for efficiency. */
19844
9e4d1a6307f6 More on SF bug [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19839
diff changeset
2127 if (PyTuple_CheckExact(v)) {
9e4d1a6307f6 More on SF bug [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19839
diff changeset
2128 /* Note that we can't know whether it's safe to return
9e4d1a6307f6 More on SF bug [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19839
diff changeset
2129 a tuple *subclass* instance as-is, hence the restriction
19845
7a980c98c8ae Fix tortured comment -- I must be on drugs today.
Tim Peters <tim.peters@gmail.com>
parents: 19844
diff changeset
2130 to exact tuples here. In contrast, lists always make
7a980c98c8ae Fix tortured comment -- I must be on drugs today.
Tim Peters <tim.peters@gmail.com>
parents: 19844
diff changeset
2131 a copy, so there's no need for exactness below. */
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2132 Py_INCREF(v);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2133 return v;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2134 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2135 if (PyList_Check(v))
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2136 return PyList_AsTuple(v);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2137
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2138 /* Get iterator. */
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2139 it = PyObject_GetIter(v);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2140 if (it == NULL)
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20604
diff changeset
2141 return NULL;
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2142
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2143 /* Guess result size and allocate space. */
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 44171
diff changeset
2144 n = _PyObject_LengthHint(v, 10);
51282
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
2145 if (n == -1)
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48876
diff changeset
2146 goto Fail;
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2147 result = PyTuple_New(n);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2148 if (result == NULL)
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2149 goto Fail;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2150
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2151 /* Fill the tuple. */
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2152 for (j = 0; ; ++j) {
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2153 PyObject *item = PyIter_Next(it);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2154 if (item == NULL) {
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2155 if (PyErr_Occurred())
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2156 goto Fail;
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2157 break;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2158 }
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2159 if (j >= n) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2160 Py_ssize_t oldn = n;
34172
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2161 /* The over-allocation strategy can grow a bit faster
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2162 than for lists because unlike lists the
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2163 over-allocation isn't permanent -- we reclaim
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2164 the excess before the end of this routine.
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2165 So, grow by ten and then add 25%.
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2166 */
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2167 n += 10;
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2168 n += n >> 2;
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2169 if (n < oldn) {
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2170 /* Check for overflow */
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2171 PyErr_NoMemory();
34173
2144a352aa37 Add missing decref.
Raymond Hettinger <python@rcn.com>
parents: 34172
diff changeset
2172 Py_DECREF(item);
34172
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2173 goto Fail;
13021d351358 SF bug #1085744: Performance issues with PySequence_Tuple()
Raymond Hettinger <python@rcn.com>
parents: 33521
diff changeset
2174 }
17976
d579a5d8b3fe Cruft cleanup: Removed the unused last_is_sticky argument from the internal
Tim Peters <tim.peters@gmail.com>
parents: 17692
diff changeset
2175 if (_PyTuple_Resize(&result, n) != 0) {
17685
0c9877120f38 Fix a tiny and unlikely memory leak. Was there before too, and actually
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
2176 Py_DECREF(item);
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2177 goto Fail;
17685
0c9877120f38 Fix a tiny and unlikely memory leak. Was there before too, and actually
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
2178 }
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2179 }
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2180 PyTuple_SET_ITEM(result, j, item);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2181 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2182
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2183 /* Cut tuple back if guess was too large. */
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2184 if (j < n &&
17976
d579a5d8b3fe Cruft cleanup: Removed the unused last_is_sticky argument from the internal
Tim Peters <tim.peters@gmail.com>
parents: 17692
diff changeset
2185 _PyTuple_Resize(&result, j) != 0)
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2186 goto Fail;
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2187
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2188 Py_DECREF(it);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2189 return result;
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2190
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2191 Fail:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2192 Py_XDECREF(result);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2193 Py_DECREF(it);
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2194 return NULL;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2195 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2196
4582
2f8d9c22de62 Added missing PySequence_List.
Guido van Rossum <guido@python.org>
parents: 4529
diff changeset
2197 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2198 PySequence_List(PyObject *v)
4582
2f8d9c22de62 Added missing PySequence_List.
Guido van Rossum <guido@python.org>
parents: 4529
diff changeset
2199 {
17648
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2200 PyObject *result; /* result list */
31642
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2201 PyObject *rv; /* return value from PyList_Extend */
5235
4f1fa7b69ba7 Several fixes reported by jim F.
Guido van Rossum <guido@python.org>
parents: 4771
diff changeset
2202
8662
74f385ee44cb Add special case to PySequence_List() so that list() of a list is
Guido van Rossum <guido@python.org>
parents: 8660
diff changeset
2203 if (v == NULL)
74f385ee44cb Add special case to PySequence_List() so that list() of a list is
Guido van Rossum <guido@python.org>
parents: 8660
diff changeset
2204 return null_error();
74f385ee44cb Add special case to PySequence_List() so that list() of a list is
Guido van Rossum <guido@python.org>
parents: 8660
diff changeset
2205
31642
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2206 result = PyList_New(0);
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2207 if (result == NULL)
17648
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2208 return NULL;
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2209
31642
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2210 rv = _PyList_Extend((PyListObject *)result, v);
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2211 if (rv == NULL) {
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31370
diff changeset
2212 Py_DECREF(result);
17648
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2213 return NULL;
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2214 }
31676
2bfc8866ea99 Add missing decref
Raymond Hettinger <python@rcn.com>
parents: 31642
diff changeset
2215 Py_DECREF(rv);
17648
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17617
diff changeset
2216 return result;
4582
2f8d9c22de62 Added missing PySequence_List.
Guido van Rossum <guido@python.org>
parents: 4529
diff changeset
2217 }
2f8d9c22de62 Added missing PySequence_List.
Guido van Rossum <guido@python.org>
parents: 4529
diff changeset
2218
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2219 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2220 PySequence_Fast(PyObject *v, const char *m)
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2221 {
31370
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2222 PyObject *it;
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2223
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2224 if (v == NULL)
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2225 return null_error();
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2226
26042
4b8044d1f1ac Use PyList_CheckExact and PyTuple_CheckExact for checking whether
Michael W. Hudson <mwh@python.net>
parents: 24867
diff changeset
2227 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2228 Py_INCREF(v);
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2229 return v;
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2230 }
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2231
31370
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2232 it = PyObject_GetIter(v);
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2233 if (it == NULL) {
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2234 if (PyErr_ExceptionMatches(PyExc_TypeError))
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2235 PyErr_SetString(PyExc_TypeError, m);
31370
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2236 return NULL;
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2237 }
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2238
34182
4714aff4bf75 Small boost to PySequence_Fast(). Lists build faster than tuples for
Raymond Hettinger <python@rcn.com>
parents: 34173
diff changeset
2239 v = PySequence_List(it);
31370
a6284e9f30a2 SF Patch #871704: Py_SequenceFast can mask errors
Raymond Hettinger <python@rcn.com>
parents: 31345
diff changeset
2240 Py_DECREF(it);
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2241
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2242 return v;
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2243 }
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12248
diff changeset
2244
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2245 /* Iterate over seq. Result depends on the operation:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2246 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 51466
diff changeset
2247 PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2248 set ValueError and return -1 if none found; also return -1 on error.
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2249 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2250 */
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2251 Py_ssize_t
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2252 _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2253 {
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2254 Py_ssize_t n;
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2255 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2256 PyObject *it; /* iter(seq) */
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2257
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2258 if (seq == NULL || obj == NULL) {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2259 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2260 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2261 }
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2262
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2263 it = PyObject_GetIter(seq);
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2264 if (it == NULL) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2265 type_error("argument of type '%.200s' is not iterable", seq);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2266 return -1;
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2267 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2268
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2269 n = wrapped = 0;
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2270 for (;;) {
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2271 int cmp;
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2272 PyObject *item = PyIter_Next(it);
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2273 if (item == NULL) {
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2274 if (PyErr_Occurred())
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2275 goto Fail;
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2276 break;
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2277 }
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2278
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2279 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2280 Py_DECREF(item);
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2281 if (cmp < 0)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2282 goto Fail;
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2283 if (cmp > 0) {
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2284 switch (operation) {
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2285 case PY_ITERSEARCH_COUNT:
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2286 if (n == PY_SSIZE_T_MAX) {
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2287 PyErr_SetString(PyExc_OverflowError,
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2288 "count exceeds C integer size");
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2289 goto Fail;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2290 }
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2291 ++n;
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2292 break;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2293
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2294 case PY_ITERSEARCH_INDEX:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2295 if (wrapped) {
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2296 PyErr_SetString(PyExc_OverflowError,
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2297 "index exceeds C integer size");
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2298 goto Fail;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2299 }
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2300 goto Done;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2301
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2302 case PY_ITERSEARCH_CONTAINS:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2303 n = 1;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2304 goto Done;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2305
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2306 default:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2307 assert(!"unknown operation");
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2308 }
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2309 }
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2310
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2311 if (operation == PY_ITERSEARCH_INDEX) {
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2312 if (n == PY_SSIZE_T_MAX)
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39507
diff changeset
2313 wrapped = 1;
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2314 ++n;
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2315 }
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2316 }
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2317
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2318 if (operation != PY_ITERSEARCH_INDEX)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2319 goto Done;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2320
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2321 PyErr_SetString(PyExc_ValueError,
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2322 "sequence.index(x): x not in sequence");
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2323 /* fall into failure code */
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2324 Fail:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2325 n = -1;
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2326 /* fall through */
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2327 Done:
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2328 Py_DECREF(it);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2329 return n;
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
2330
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2331 }
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2332
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2333 /* Return # of times o appears in s. */
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2334 Py_ssize_t
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2335 PySequence_Count(PyObject *s, PyObject *o)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2336 {
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2337 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2338 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2339
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2340 /* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2341 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2342 */
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2343 int
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2344 PySequence_Contains(PyObject *seq, PyObject *ob)
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2345 {
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2346 Py_ssize_t result;
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2347 if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2348 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2349 if (sqm != NULL && sqm->sq_contains != NULL)
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2350 return (*sqm->sq_contains)(seq, ob);
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2351 }
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2352 result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2353 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2354 }
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
2355
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2356 /* Backwards compatibility */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2357 #undef PySequence_In
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2358 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2359 PySequence_In(PyObject *w, PyObject *v)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2360 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2361 return PySequence_Contains(w, v);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2362 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2363
36834
0b7809660456 Change some sequnce APIs to use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36675
diff changeset
2364 Py_ssize_t
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2365 PySequence_Index(PyObject *s, PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2366 {
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19804
diff changeset
2367 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2368 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2369
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2370 /* Operations on mappings */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2371
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2372 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2373 PyMapping_Check(PyObject *o)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2374 {
33521
55eca7cd3531 SF bug #1030557: PyMapping_Check crashes when argument is NULL
Raymond Hettinger <python@rcn.com>
parents: 32759
diff changeset
2375 if (o && PyInstance_Check(o))
31870
c7f9fca946d1 Improve accuracy of sequence and mapping checks.
Raymond Hettinger <python@rcn.com>
parents: 31734
diff changeset
2376 return PyObject_HasAttrString(o, "__getitem__");
c7f9fca946d1 Improve accuracy of sequence and mapping checks.
Raymond Hettinger <python@rcn.com>
parents: 31734
diff changeset
2377
c7f9fca946d1 Improve accuracy of sequence and mapping checks.
Raymond Hettinger <python@rcn.com>
parents: 31734
diff changeset
2378 return o && o->ob_type->tp_as_mapping &&
c7f9fca946d1 Improve accuracy of sequence and mapping checks.
Raymond Hettinger <python@rcn.com>
parents: 31734
diff changeset
2379 o->ob_type->tp_as_mapping->mp_subscript &&
31871
020dc5dfd5c5 Improve previous checkin to use a slot check instead of equivalent
Raymond Hettinger <python@rcn.com>
parents: 31870
diff changeset
2380 !(o->ob_type->tp_as_sequence &&
020dc5dfd5c5 Improve previous checkin to use a slot check instead of equivalent
Raymond Hettinger <python@rcn.com>
parents: 31870
diff changeset
2381 o->ob_type->tp_as_sequence->sq_slice);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2382 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2383
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2384 Py_ssize_t
13575
7c119f050290 change abstract size functions PySequence_Size &c.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13451
diff changeset
2385 PyMapping_Size(PyObject *o)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2386 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2387 PyMappingMethods *m;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2388
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2389 if (o == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2390 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2391 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2392 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2393
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2394 m = o->ob_type->tp_as_mapping;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2395 if (m && m->mp_length)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2396 return m->mp_length(o);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2397
39394
f5a7f4dd93e0 Remove "non-mapping" and "non-sequence" from TypeErrors raised by
Georg Brandl <georg@python.org>
parents: 38904
diff changeset
2398 type_error("object of type '%.200s' has no len()", o);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2399 return -1;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2400 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2401
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2402 #undef PyMapping_Length
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2403 Py_ssize_t
13634
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2404 PyMapping_Length(PyObject *o)
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2405 {
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2406 return PyMapping_Size(o);
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2407 }
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2408 #define PyMapping_Length PyMapping_Size
6f0d96e6b5a5 Restore PyXXX_Length() APIs for binary compatibility.
Marc-André Lemburg <mal@egenix.com>
parents: 13575
diff changeset
2409
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2410 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2411 PyMapping_GetItemString(PyObject *o, char *key)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2412 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2413 PyObject *okey, *r;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2414
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2415 if (key == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2416 return null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2417
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
2418 okey = PyString_FromString(key);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2419 if (okey == NULL)
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2420 return NULL;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2421 r = PyObject_GetItem(o, okey);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2422 Py_DECREF(okey);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2423 return r;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2424 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2425
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2426 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2427 PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2428 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2429 PyObject *okey;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2430 int r;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2431
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2432 if (key == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2433 null_error();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2434 return -1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2435 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2436
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
2437 okey = PyString_FromString(key);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2438 if (okey == NULL)
8452
84d1b7cf815a Fix a whole bunch of error return NULL that should be return -1.
Guido van Rossum <guido@python.org>
parents: 8437
diff changeset
2439 return -1;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2440 r = PyObject_SetItem(o, okey, value);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2441 Py_DECREF(okey);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2442 return r;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2443 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2444
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2445 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2446 PyMapping_HasKeyString(PyObject *o, char *key)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2447 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2448 PyObject *v;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2449
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2450 v = PyMapping_GetItemString(o, key);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2451 if (v) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2452 Py_DECREF(v);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2453 return 1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2454 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2455 PyErr_Clear();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2456 return 0;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2457 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2458
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2459 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2460 PyMapping_HasKey(PyObject *o, PyObject *key)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2461 {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2462 PyObject *v;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2463
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2464 v = PyObject_GetItem(o, key);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2465 if (v) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2466 Py_DECREF(v);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2467 return 1;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2468 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2469 PyErr_Clear();
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2470 return 0;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2471 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2472
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2473 /* Operations on callable objects */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2474
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2475 /* XXX PyCallable_Check() is in object.c */
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2476
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2477 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2478 PyObject_CallObject(PyObject *o, PyObject *a)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2479 {
19910
49b7e3980853 PyObject_CallObject(): this may as well call PyEval_CallObject()
Guido van Rossum <guido@python.org>
parents: 19845
diff changeset
2480 return PyEval_CallObjectWithKeywords(o, a, NULL);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2481 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2482
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2483 PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2484 PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2485 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2486 ternaryfunc call;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2487
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2488 if ((call = func->ob_type->tp_call) != NULL) {
43276
3bc3f12cae28 Fix a crasher where Python code managed to infinitely recurse in C code without
Brett Cannon <bcannon@gmail.com>
parents: 40764
diff changeset
2489 PyObject *result;
3bc3f12cae28 Fix a crasher where Python code managed to infinitely recurse in C code without
Brett Cannon <bcannon@gmail.com>
parents: 40764
diff changeset
2490 if (Py_EnterRecursiveCall(" while calling a Python object"))
3bc3f12cae28 Fix a crasher where Python code managed to infinitely recurse in C code without
Brett Cannon <bcannon@gmail.com>
parents: 40764
diff changeset
2491 return NULL;
3bc3f12cae28 Fix a crasher where Python code managed to infinitely recurse in C code without
Brett Cannon <bcannon@gmail.com>
parents: 40764
diff changeset
2492 result = (*call)(func, arg, kw);
3bc3f12cae28 Fix a crasher where Python code managed to infinitely recurse in C code without
Brett Cannon <bcannon@gmail.com>
parents: 40764
diff changeset
2493 Py_LeaveRecursiveCall();
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2494 if (result == NULL && !PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2495 PyErr_SetString(
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2496 PyExc_SystemError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2497 "NULL result without error in PyObject_Call");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2498 return result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2499 }
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2500 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
20824
b3d8d2615b23 PyFunction_Call() did not check the result of PyObject_Repr() for NULL, and
Fred Drake <fdrake@acm.org>
parents: 20729
diff changeset
2501 func->ob_type->tp_name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2502 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2503 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2504
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2505 static PyObject*
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2506 call_function_tail(PyObject *callable, PyObject *args)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2507 {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2508 PyObject *retval;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2509
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2510 if (args == NULL)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2511 return NULL;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2512
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2513 if (!PyTuple_Check(args)) {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2514 PyObject *a;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2515
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2516 a = PyTuple_New(1);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2517 if (a == NULL) {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2518 Py_DECREF(args);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2519 return NULL;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2520 }
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2521 PyTuple_SET_ITEM(a, 0, args);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2522 args = a;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2523 }
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2524 retval = PyObject_Call(callable, args, NULL);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2525
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2526 Py_DECREF(args);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2527
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2528 return retval;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2529 }
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2530
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2531 PyObject *
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2532 PyObject_CallFunction(PyObject *callable, char *format, ...)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2533 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2534 va_list va;
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2535 PyObject *args;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2536
20719
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2537 if (callable == NULL)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2538 return null_error();
20719
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2539
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2540 if (format && *format) {
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2541 va_start(va, format);
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2542 args = Py_VaBuildValue(format, va);
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2543 va_end(va);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2544 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2545 else
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2546 args = PyTuple_New(0);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2547
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2548 return call_function_tail(callable, args);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2549 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2550
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2551 PyObject *
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2552 _PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2553 {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2554 va_list va;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2555 PyObject *args;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2556
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2557 if (callable == NULL)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2558 return null_error();
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2559
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2560 if (format && *format) {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2561 va_start(va, format);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2562 args = _Py_VaBuildValue_SizeT(format, va);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2563 va_end(va);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2564 }
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2565 else
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2566 args = PyTuple_New(0);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2567
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2568 return call_function_tail(callable, args);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2569 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2570
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2571 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2572 PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2573 {
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2574 va_list va;
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2575 PyObject *args;
35225
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2576 PyObject *func = NULL;
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2577 PyObject *retval = NULL;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2578
20719
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2579 if (o == NULL || name == NULL)
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2580 return null_error();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2581
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2582 func = PyObject_GetAttrString(o, name);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2583 if (func == NULL) {
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2584 PyErr_SetString(PyExc_AttributeError, name);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2585 return 0;
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2586 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2587
35225
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2588 if (!PyCallable_Check(func)) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2589 type_error("attribute of type '%.200s' is not callable", func);
35225
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2590 goto exit;
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2591 }
20719
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2592
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2593 if (format && *format) {
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2594 va_start(va, format);
5e347e4e05bf PyObject_CallFunction(), PyObject_CallMethod(): Make sure we do not touch
Fred Drake <fdrake@acm.org>
parents: 20694
diff changeset
2595 args = Py_VaBuildValue(format, va);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2596 va_end(va);
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2597 }
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2598 else
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2599 args = PyTuple_New(0);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2600
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2601 retval = call_function_tail(func, args);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2602
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2603 exit:
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2604 /* args gets consumed in call_function_tail */
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2605 Py_XDECREF(func);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2606
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2607 return retval;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2608 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2609
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2610 PyObject *
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2611 _PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2612 {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2613 va_list va;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2614 PyObject *args;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2615 PyObject *func = NULL;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2616 PyObject *retval = NULL;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2617
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2618 if (o == NULL || name == NULL)
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2619 return null_error();
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2620
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2621 func = PyObject_GetAttrString(o, name);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2622 if (func == NULL) {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2623 PyErr_SetString(PyExc_AttributeError, name);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2624 return 0;
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2625 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2626
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2627 if (!PyCallable_Check(func)) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
2628 type_error("attribute of type '%.200s' is not callable", func);
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2629 goto exit;
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2630 }
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2631
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2632 if (format && *format) {
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2633 va_start(va, format);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2634 args = _Py_VaBuildValue_SizeT(format, va);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2635 va_end(va);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2636 }
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2637 else
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2638 args = PyTuple_New(0);
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2639
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2640 retval = call_function_tail(func, args);
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2641
35225
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2642 exit:
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37308
diff changeset
2643 /* args gets consumed in call_function_tail */
35225
Michael W. Hudson <mwh@python.net>
parents: 34931
diff changeset
2644 Py_XDECREF(func);
8400
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2645
6270c6bad811 Completely reformatted, standardizing indentation as well as
Guido van Rossum <guido@python.org>
parents: 7854
diff changeset
2646 return retval;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2647 }
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2648
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2649
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2650 static PyObject *
20729
cd600039a123 PyObject_CallFunctionObArgs() ---> PyObject_CallFunctionObjArgs()
Fred Drake <fdrake@acm.org>
parents: 20719
diff changeset
2651 objargs_mktuple(va_list va)
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2652 {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2653 int i, n = 0;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2654 va_list countva;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2655 PyObject *result, *tmp;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2656
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2657 #ifdef VA_LIST_IS_ARRAY
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2658 memcpy(countva, va, sizeof(va_list));
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2659 #else
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 23703
diff changeset
2660 #ifdef __va_copy
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 23703
diff changeset
2661 __va_copy(countva, va);
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 23703
diff changeset
2662 #else
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2663 countva = va;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2664 #endif
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 23703
diff changeset
2665 #endif
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2666
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2667 while (((PyObject *)va_arg(countva, PyObject *)) != NULL)
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2668 ++n;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2669 result = PyTuple_New(n);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2670 if (result != NULL && n > 0) {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2671 for (i = 0; i < n; ++i) {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2672 tmp = (PyObject *)va_arg(va, PyObject *);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2673 PyTuple_SET_ITEM(result, i, tmp);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2674 Py_INCREF(tmp);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2675 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2676 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2677 return result;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2678 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2679
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2680 PyObject *
20729
cd600039a123 PyObject_CallFunctionObArgs() ---> PyObject_CallFunctionObjArgs()
Fred Drake <fdrake@acm.org>
parents: 20719
diff changeset
2681 PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2682 {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2683 PyObject *args, *tmp;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2684 va_list vargs;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2685
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2686 if (callable == NULL || name == NULL)
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2687 return null_error();
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2688
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2689 callable = PyObject_GetAttr(callable, name);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2690 if (callable == NULL)
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2691 return NULL;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2692
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2693 /* count the args */
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2694 va_start(vargs, name);
20729
cd600039a123 PyObject_CallFunctionObArgs() ---> PyObject_CallFunctionObjArgs()
Fred Drake <fdrake@acm.org>
parents: 20719
diff changeset
2695 args = objargs_mktuple(vargs);
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2696 va_end(vargs);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2697 if (args == NULL) {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2698 Py_DECREF(callable);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2699 return NULL;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2700 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2701 tmp = PyObject_Call(callable, args, NULL);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2702 Py_DECREF(args);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2703 Py_DECREF(callable);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2704
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2705 return tmp;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2706 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2707
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2708 PyObject *
20729
cd600039a123 PyObject_CallFunctionObArgs() ---> PyObject_CallFunctionObjArgs()
Fred Drake <fdrake@acm.org>
parents: 20719
diff changeset
2709 PyObject_CallFunctionObjArgs(PyObject *callable, ...)
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2710 {
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2711 PyObject *args, *tmp;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2712 va_list vargs;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2713
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2714 if (callable == NULL)
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2715 return null_error();
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2716
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2717 /* count the args */
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2718 va_start(vargs, callable);
20729
cd600039a123 PyObject_CallFunctionObArgs() ---> PyObject_CallFunctionObjArgs()
Fred Drake <fdrake@acm.org>
parents: 20719
diff changeset
2719 args = objargs_mktuple(vargs);
20694
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2720 va_end(vargs);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2721 if (args == NULL)
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2722 return NULL;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2723 tmp = PyObject_Call(callable, args, NULL);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2724 Py_DECREF(args);
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2725
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2726 return tmp;
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2727 }
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2728
d02068d163cf Added two new functions to conveniently call functions/methods from C.
Fred Drake <fdrake@acm.org>
parents: 20683
diff changeset
2729
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2730 /* isinstance(), issubclass() */
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2731
23260
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2732 /* abstract_get_bases() has logically 4 return states, with a sort of 0th
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2733 * state that will almost never happen.
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2734 *
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2735 * 0. creating the __bases__ static string could get a MemoryError
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2736 * 1. getattr(cls, '__bases__') could raise an AttributeError
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2737 * 2. getattr(cls, '__bases__') could raise some other exception
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2738 * 3. getattr(cls, '__bases__') could return a tuple
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2739 * 4. getattr(cls, '__bases__') could return something other than a tuple
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2740 *
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2741 * Only state #3 is a non-error state and only it returns a non-NULL object
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2742 * (it returns the retrieved tuple).
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2743 *
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2744 * Any raised AttributeErrors are masked by clearing the exception and
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2745 * returning NULL. If an object other than a tuple comes out of __bases__,
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2746 * then again, the return value is NULL. So yes, these two situations
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2747 * produce exactly the same results: NULL is returned and no error is set.
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2748 *
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2749 * If some exception other than AttributeError is raised, then NULL is also
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2750 * returned, but the exception is not cleared. That's because we want the
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2751 * exception to be propagated along.
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2752 *
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2753 * Callers are expected to test for PyErr_Occurred() when the return value
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2754 * is NULL to decide whether a valid exception should be propagated or not.
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2755 * When there's no exception to propagate, it's customary for the caller to
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2756 * set a TypeError.
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2757 */
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2758 static PyObject *
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2759 abstract_get_bases(PyObject *cls)
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2760 {
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2761 static PyObject *__bases__ = NULL;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2762 PyObject *bases;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2763
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2764 if (__bases__ == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
2765 __bases__ = PyString_InternFromString("__bases__");
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2766 if (__bases__ == NULL)
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2767 return NULL;
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2768 }
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2769 bases = PyObject_GetAttr(cls, __bases__);
23260
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2770 if (bases == NULL) {
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2771 if (PyErr_ExceptionMatches(PyExc_AttributeError))
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2772 PyErr_Clear();
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2773 return NULL;
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2774 }
23260
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2775 if (!PyTuple_Check(bases)) {
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2776 Py_DECREF(bases);
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2777 return NULL;
9e3532b75471 abstract_get_bases(): Clarify exactly what the return values and
Barry Warsaw <barry@python.org>
parents: 23090
diff changeset
2778 }
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2779 return bases;
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2780 }
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2781
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2782
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2783 static int
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2784 abstract_issubclass(PyObject *derived, PyObject *cls)
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2785 {
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2786 PyObject *bases = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2787 Py_ssize_t i, n;
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2788 int r = 0;
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2789
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2790 while (1) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2791 if (derived == cls)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2792 return 1;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2793 bases = abstract_get_bases(derived);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2794 if (bases == NULL) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2795 if (PyErr_Occurred())
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2796 return -1;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2797 return 0;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2798 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2799 n = PyTuple_GET_SIZE(bases);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2800 if (n == 0) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2801 Py_DECREF(bases);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2802 return 0;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2803 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2804 /* Avoid recursivity in the single inheritance case */
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2805 if (n == 1) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2806 derived = PyTuple_GET_ITEM(bases, 0);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2807 Py_DECREF(bases);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2808 continue;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2809 }
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2810 for (i = 0; i < n; i++) {
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2811 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2812 if (r != 0)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2813 break;
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2814 }
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2815 Py_DECREF(bases);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2816 return r;
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2817 }
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2818 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2819
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2820 static int
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2821 check_class(PyObject *cls, const char *error)
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2822 {
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2823 PyObject *bases = abstract_get_bases(cls);
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2824 if (bases == NULL) {
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2825 /* Do not mask errors. */
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2826 if (!PyErr_Occurred())
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2827 PyErr_SetString(PyExc_TypeError, error);
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2828 return 0;
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2829 }
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2830 Py_DECREF(bases);
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2831 return -1;
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2832 }
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2833
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2834 static int
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2835 recursive_isinstance(PyObject *inst, PyObject *cls)
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2836 {
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2837 PyObject *icls;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2838 static PyObject *__class__ = NULL;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2839 int retval = 0;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2840
27829
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2841 if (__class__ == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
2842 __class__ = PyString_InternFromString("__class__");
27829
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2843 if (__class__ == NULL)
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2844 return -1;
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2845 }
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2846
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2847 if (PyClass_Check(cls) && PyInstance_Check(inst)) {
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2848 PyObject *inclass =
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2849 (PyObject*)((PyInstanceObject*)inst)->in_class;
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2850 retval = PyClass_IsSubclass(inclass, cls);
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2851 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2852 else if (PyType_Check(cls)) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17976
diff changeset
2853 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
27829
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2854 if (retval == 0) {
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2855 PyObject *c = PyObject_GetAttr(inst, __class__);
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2856 if (c == NULL) {
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2857 PyErr_Clear();
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2858 }
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2859 else {
27830
bf9ca086b8aa Add missing cast in previous fix.
Guido van Rossum <guido@python.org>
parents: 27829
diff changeset
2860 if (c != (PyObject *)(inst->ob_type) &&
bf9ca086b8aa Add missing cast in previous fix.
Guido van Rossum <guido@python.org>
parents: 27829
diff changeset
2861 PyType_Check(c))
27829
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2862 retval = PyType_IsSubtype(
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2863 (PyTypeObject *)c,
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2864 (PyTypeObject *)cls);
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2865 Py_DECREF(c);
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2866 }
4a02ca6bdb6f SF #532767: isinstance(x, X) should work when x is a proxy for an X
Guido van Rossum <guido@python.org>
parents: 27793
diff changeset
2867 }
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2868 }
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2869 else {
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2870 if (!check_class(cls,
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2871 "isinstance() arg 2 must be a class, type,"
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2872 " or tuple of classes and types"))
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2873 return -1;
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2874 icls = PyObject_GetAttr(inst, __class__);
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2875 if (icls == NULL) {
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2876 PyErr_Clear();
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2877 retval = 0;
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2878 }
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2879 else {
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2880 retval = abstract_issubclass(icls, cls);
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2881 Py_DECREF(icls);
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2882 }
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2883 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2884
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2885 return retval;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2886 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2887
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2888 int
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2889 PyObject_IsInstance(PyObject *inst, PyObject *cls)
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2890 {
45356
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2891 static PyObject *name = NULL;
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2892 PyObject *checker;
45889
2385e16acb42 Speed-up isinstance() for one easy case.
Raymond Hettinger <python@rcn.com>
parents: 45832
diff changeset
2893
2385e16acb42 Speed-up isinstance() for one easy case.
Raymond Hettinger <python@rcn.com>
parents: 45832
diff changeset
2894 /* Quick test for an exact match */
45904
d5cff7f9a142 Fix compiler warning.
Raymond Hettinger <python@rcn.com>
parents: 45889
diff changeset
2895 if (Py_TYPE(inst) == (PyTypeObject *)cls)
45889
2385e16acb42 Speed-up isinstance() for one easy case.
Raymond Hettinger <python@rcn.com>
parents: 45832
diff changeset
2896 return 1;
2385e16acb42 Speed-up isinstance() for one easy case.
Raymond Hettinger <python@rcn.com>
parents: 45832
diff changeset
2897
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2898 if (PyTuple_Check(cls)) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2899 Py_ssize_t i;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2900 Py_ssize_t n;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2901 int r = 0;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2902
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2903 if (Py_EnterRecursiveCall(" in __instancecheck__"))
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2904 return -1;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2905 n = PyTuple_GET_SIZE(cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2906 for (i = 0; i < n; ++i) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2907 PyObject *item = PyTuple_GET_ITEM(cls, i);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2908 r = PyObject_IsInstance(inst, item);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2909 if (r != 0)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2910 /* either found it, or got an error */
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2911 break;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2912 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2913 Py_LeaveRecursiveCall();
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2914 return r;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2915 }
45356
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2916 if (name == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
2917 name = PyString_InternFromString("__instancecheck__");
45356
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2918 if (name == NULL)
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2919 return -1;
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2920 }
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2921 checker = PyObject_GetAttr(cls, name);
45936
3aa5eb182913 Remove unnecessary traceback save/restore pair.
Raymond Hettinger <python@rcn.com>
parents: 45904
diff changeset
2922 if (checker == NULL && PyErr_Occurred())
3aa5eb182913 Remove unnecessary traceback save/restore pair.
Raymond Hettinger <python@rcn.com>
parents: 45904
diff changeset
2923 PyErr_Clear();
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2924 if (checker != NULL) {
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2925 PyObject *res;
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2926 int ok = -1;
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2927 if (Py_EnterRecursiveCall(" in __instancecheck__")) {
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2928 Py_DECREF(checker);
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2929 return ok;
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2930 }
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2931 res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2932 Py_LeaveRecursiveCall();
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2933 Py_DECREF(checker);
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2934 if (res != NULL) {
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2935 ok = PyObject_IsTrue(res);
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2936 Py_DECREF(res);
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2937 }
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2938 return ok;
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2939 }
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2940 return recursive_isinstance(inst, cls);
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2941 }
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2942
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2943 static int
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2944 recursive_issubclass(PyObject *derived, PyObject *cls)
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2945 {
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2946 int retval;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2947
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2948 if (PyType_Check(cls) && PyType_Check(derived)) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2949 /* Fast path (non-recursive) */
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2950 return PyType_IsSubtype(
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2951 (PyTypeObject *)derived, (PyTypeObject *)cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2952 }
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2953 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
27793
f0409d754b93 Fold long lines.
Guido van Rossum <guido@python.org>
parents: 26987
diff changeset
2954 if (!check_class(derived,
f0409d754b93 Fold long lines.
Guido van Rossum <guido@python.org>
parents: 26987
diff changeset
2955 "issubclass() arg 1 must be a class"))
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 26567
diff changeset
2956 return -1;
23089
314f92a24293 Whitespace normalization and fold some long lines.
Guido van Rossum <guido@python.org>
parents: 22194
diff changeset
2957
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2958 if (!check_class(cls,
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2959 "issubclass() arg 2 must be a class"
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2960 " or tuple of classes"))
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2961 return -1;
20507
364c238a6871 Fix error checking done by abstract_issubclass and abstract_isinstance.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20307
diff changeset
2962 retval = abstract_issubclass(derived, cls);
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2963 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2964 else {
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2965 /* shortcut */
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2966 if (!(retval = (derived == cls)))
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2967 retval = PyClass_IsSubclass(derived, cls);
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2968 }
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2969
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2970 return retval;
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 15944
diff changeset
2971 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
2972
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2973 int
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2974 PyObject_IsSubclass(PyObject *derived, PyObject *cls)
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
2975 {
45356
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2976 static PyObject *name = NULL;
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2977 PyObject *t, *v, *tb;
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
2978 PyObject *checker;
45356
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2979
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2980 if (PyTuple_Check(cls)) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2981 Py_ssize_t i;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2982 Py_ssize_t n;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2983 int r = 0;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2984
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2985 if (Py_EnterRecursiveCall(" in __subclasscheck__"))
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2986 return -1;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2987 n = PyTuple_GET_SIZE(cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2988 for (i = 0; i < n; ++i) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2989 PyObject *item = PyTuple_GET_ITEM(cls, i);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2990 r = PyObject_IsSubclass(derived, item);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2991 if (r != 0)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2992 /* either found it, or got an error */
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2993 break;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2994 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2995 Py_LeaveRecursiveCall();
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2996 return r;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
2997 }
45356
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
2998 if (name == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47504
diff changeset
2999 name = PyString_InternFromString("__subclasscheck__");
45356
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
3000 if (name == NULL)
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
3001 return -1;
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
3002 }
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3003 PyErr_Fetch(&t, &v, &tb);
45356
eec67af4d7df Use a static and interned string for __subclasscheck__ and __instancecheck__ as suggested by Thomas Heller in #2115
Christian Heimes <christian@cheimes.de>
parents: 45262
diff changeset
3004 checker = PyObject_GetAttr(cls, name);
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3005 PyErr_Restore(t, v, tb);
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3006 if (checker != NULL) {
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3007 PyObject *res;
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3008 int ok = -1;
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3009 if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3010 Py_DECREF(checker);
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3011 return ok;
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3012 }
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3013 res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3014 Py_LeaveRecursiveCall();
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3015 Py_DECREF(checker);
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3016 if (res != NULL) {
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3017 ok = PyObject_IsTrue(res);
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3018 Py_DECREF(res);
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3019 }
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3020 return ok;
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3021 }
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3022 return recursive_issubclass(derived, cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3023 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3024
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3025 int
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3026 _PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3027 {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3028 return recursive_isinstance(inst, cls);
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3029 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3030
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3031 int
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3032 _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3033 {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48236
diff changeset
3034 return recursive_issubclass(derived, cls);
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
3035 }
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
3036
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 31676
diff changeset
3037
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3038 PyObject *
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3039 PyObject_GetIter(PyObject *o)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3040 {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3041 PyTypeObject *t = o->ob_type;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3042 getiterfunc f = NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3043 if (PyType_HasFeature(t, Py_TPFLAGS_HAVE_ITER))
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3044 f = t->tp_iter;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3045 if (f == NULL) {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3046 if (PySequence_Check(o))
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3047 return PySeqIter_New(o);
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38707
diff changeset
3048 return type_error("'%.200s' object is not iterable", o);
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3049 }
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3050 else {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3051 PyObject *res = (*f)(o);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3052 if (res != NULL && !PyIter_Check(res)) {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3053 PyErr_Format(PyExc_TypeError,
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3054 "iter() returned non-iterator "
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3055 "of type '%.100s'",
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3056 res->ob_type->tp_name);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3057 Py_DECREF(res);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3058 res = NULL;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3059 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3060 return res;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3061 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17111
diff changeset
3062 }
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3063
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3064 /* Return next item.
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3065 * If an error occurs, return NULL. PyErr_Occurred() will be true.
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3066 * If the iteration terminates normally, return NULL and clear the
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3067 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3068 * will be false.
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3069 * Else return the next object. PyErr_Occurred() will be false.
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3070 */
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3071 PyObject *
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3072 PyIter_Next(PyObject *iter)
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3073 {
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3074 PyObject *result;
28116
9378f6e551ae Removed duplicate test from inner loop.
Raymond Hettinger <python@rcn.com>
parents: 27937
diff changeset
3075 assert(PyIter_Check(iter));
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3076 result = (*iter->ob_type->tp_iternext)(iter);
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3077 if (result == NULL &&
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3078 PyErr_Occurred() &&
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3079 PyErr_ExceptionMatches(PyExc_StopIteration))
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3080 PyErr_Clear();
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17653
diff changeset
3081 return result;
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
3082 }