annotate Objects/structseq.c @ 49955:878d40468c4e legacy-trunk

Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
author Vinay Sajip <vinay_sajip@yahoo.co.uk>
date Wed, 03 Dec 2008 23:22:58 +0000
parents b1682c693b9a
children fd25030223b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Implementation helper: a struct that looks like a tuple. See timemodule
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 and posixmodule for example uses. */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #include "Python.h"
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #include "structmember.h"
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #include "structseq.h"
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 static char visible_length_key[] = "n_sequence_fields";
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 static char real_length_key[] = "n_fields";
25808
fdca87c4234e Add n_unnamed_fields into the type.
Martin v. Löwis <martin@v.loewis.de>
parents: 25806
diff changeset
10 static char unnamed_fields_key[] = "n_unnamed_fields";
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
25806
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
12 /* Fields with this name have only a field index, not a field name.
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
13 They are only allowed for indices < n_visible_fields. */
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
14 char *PyStructSequence_UnnamedField = "unnamed field";
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
15
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42951
diff changeset
16 #define VISIBLE_SIZE(op) Py_SIZE(op)
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 #define VISIBLE_SIZE_TP(tp) PyInt_AsLong( \
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 PyDict_GetItemString((tp)->tp_dict, visible_length_key))
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 #define REAL_SIZE_TP(tp) PyInt_AsLong( \
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 PyDict_GetItemString((tp)->tp_dict, real_length_key))
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42951
diff changeset
22 #define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op))
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
25808
fdca87c4234e Add n_unnamed_fields into the type.
Martin v. Löwis <martin@v.loewis.de>
parents: 25806
diff changeset
24 #define UNNAMED_FIELDS_TP(tp) PyInt_AsLong( \
fdca87c4234e Add n_unnamed_fields into the type.
Martin v. Löwis <martin@v.loewis.de>
parents: 25806
diff changeset
25 PyDict_GetItemString((tp)->tp_dict, unnamed_fields_key))
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42951
diff changeset
26 #define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op))
25808
fdca87c4234e Add n_unnamed_fields into the type.
Martin v. Löwis <martin@v.loewis.de>
parents: 25806
diff changeset
27
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 PyStructSequence_New(PyTypeObject *type)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 PyStructSequence *obj;
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
33
22978
363c7fc174ac Remove PyMalloc_New and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22446
diff changeset
34 obj = PyObject_New(PyStructSequence, type);
48847
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
35 if (obj == NULL)
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
36 return NULL;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42951
diff changeset
37 Py_SIZE(obj) = VISIBLE_SIZE_TP(type);
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 return (PyObject*) obj;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 static void
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 structseq_dealloc(PyStructSequence *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
45 Py_ssize_t i, size;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 size = REAL_SIZE(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 for (i = 0; i < size; ++i) {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 Py_XDECREF(obj->ob_item[i]);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 }
22978
363c7fc174ac Remove PyMalloc_New and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22446
diff changeset
51 PyObject_Del(obj);
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
54 static Py_ssize_t
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 structseq_length(PyStructSequence *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 return VISIBLE_SIZE(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 static PyObject*
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
61 structseq_item(PyStructSequence *obj, Py_ssize_t i)
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 if (i < 0 || i >= VISIBLE_SIZE(obj)) {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 return NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 Py_INCREF(obj->ob_item[i]);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 return obj->ob_item[i];
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 static PyObject*
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
72 structseq_slice(PyStructSequence *obj, Py_ssize_t low, Py_ssize_t high)
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 PyTupleObject *np;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
75 Py_ssize_t i;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 if (low < 0)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 low = 0;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 if (high > VISIBLE_SIZE(obj))
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 high = VISIBLE_SIZE(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 if (high < low)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 high = low;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 np = (PyTupleObject *)PyTuple_New(high-low);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 if (np == NULL)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 return NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 for(i = low; i < high; ++i) {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 PyObject *v = obj->ob_item[i];
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 Py_INCREF(v);
20780
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents: 20540
diff changeset
89 PyTuple_SET_ITEM(np, i-low, v);
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 return (PyObject *) np;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 static PyObject *
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
95 structseq_subscript(PyStructSequence *self, PyObject *item)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
96 {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
97 if (PyIndex_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
98 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
99 if (i == -1 && PyErr_Occurred())
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
100 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
101
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
102 if (i < 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
103 i += VISIBLE_SIZE(self);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
104
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
105 if (i < 0 || i >= VISIBLE_SIZE(self)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
106 PyErr_SetString(PyExc_IndexError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
107 "tuple index out of range");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
108 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
109 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
110 Py_INCREF(self->ob_item[i]);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
111 return self->ob_item[i];
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
112 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
113 else if (PySlice_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
114 Py_ssize_t start, stop, step, slicelen, cur, i;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
115 PyObject *result;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
116
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
117 if (PySlice_GetIndicesEx((PySliceObject *)item,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
118 VISIBLE_SIZE(self), &start, &stop,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
119 &step, &slicelen) < 0) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
120 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
121 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
122 if (slicelen <= 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
123 return PyTuple_New(0);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
124 result = PyTuple_New(slicelen);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
125 if (result == NULL)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
126 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
127 for (cur = start, i = 0; i < slicelen;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
128 cur += step, i++) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
129 PyObject *v = self->ob_item[cur];
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
130 Py_INCREF(v);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
131 PyTuple_SET_ITEM(result, i, v);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
132 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
133 return result;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
134 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
135 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
136 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
137 "structseq index must be integer");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
138 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
139 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
140 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
141
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
142 static PyObject *
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 PyObject *arg = NULL;
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
146 PyObject *dict = NULL;
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
147 PyObject *ob;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 PyStructSequence *res = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
149 Py_ssize_t len, min_len, max_len, i, n_unnamed_fields;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36519
diff changeset
150 static char *kwlist[] = {"sequence", "dict", 0};
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
152 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:structseq",
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
153 kwlist, &arg, &dict))
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 return NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
156 arg = PySequence_Fast(arg, "constructor requires a sequence");
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
157
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
158 if (!arg) {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
159 return NULL;
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
160 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
161
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
162 if (dict && !PyDict_Check(dict)) {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
163 PyErr_Format(PyExc_TypeError,
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
164 "%.500s() takes a dict as second arg, if any",
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
165 type->tp_name);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
166 Py_DECREF(arg);
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 return NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
170 len = PySequence_Fast_GET_SIZE(arg);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
171 min_len = VISIBLE_SIZE_TP(type);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
172 max_len = REAL_SIZE_TP(type);
25808
fdca87c4234e Add n_unnamed_fields into the type.
Martin v. Löwis <martin@v.loewis.de>
parents: 25806
diff changeset
173 n_unnamed_fields = UNNAMED_FIELDS_TP(type);
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
174
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
175 if (min_len != max_len) {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
176 if (len < min_len) {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
177 PyErr_Format(PyExc_TypeError,
36510
0f3894bb2ada Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents: 36509
diff changeset
178 "%.500s() takes an at least %zd-sequence (%zd-sequence given)",
36511
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
179 type->tp_name, min_len, len);
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
180 Py_DECREF(arg);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
181 return NULL;
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
182 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
183
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
184 if (len > max_len) {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
185 PyErr_Format(PyExc_TypeError,
36510
0f3894bb2ada Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents: 36509
diff changeset
186 "%.500s() takes an at most %zd-sequence (%zd-sequence given)",
36511
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
187 type->tp_name, max_len, len);
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
188 Py_DECREF(arg);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
189 return NULL;
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
190 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
191 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
192 else {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
193 if (len != min_len) {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
194 PyErr_Format(PyExc_TypeError,
36510
0f3894bb2ada Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents: 36509
diff changeset
195 "%.500s() takes a %zd-sequence (%zd-sequence given)",
36511
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
196 type->tp_name, min_len, len);
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
197 Py_DECREF(arg);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
198 return NULL;
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
199 }
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 res = (PyStructSequence*) PyStructSequence_New(type);
26708
3593f2b91d7d SF # 654974, fix unchecked return values in structseq
Neal Norwitz <nnorwitz@gmail.com>
parents: 25808
diff changeset
203 if (res == NULL) {
3593f2b91d7d SF # 654974, fix unchecked return values in structseq
Neal Norwitz <nnorwitz@gmail.com>
parents: 25808
diff changeset
204 return NULL;
3593f2b91d7d SF # 654974, fix unchecked return values in structseq
Neal Norwitz <nnorwitz@gmail.com>
parents: 25808
diff changeset
205 }
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 for (i = 0; i < len; ++i) {
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
207 PyObject *v = PySequence_Fast_GET_ITEM(arg, i);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
208 Py_INCREF(v);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
209 res->ob_item[i] = v;
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
210 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
211 for (; i < max_len; ++i) {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
212 if (dict && (ob = PyDict_GetItemString(
25808
fdca87c4234e Add n_unnamed_fields into the type.
Martin v. Löwis <martin@v.loewis.de>
parents: 25806
diff changeset
213 dict, type->tp_members[i-n_unnamed_fields].name))) {
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
214 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
215 else {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
216 ob = Py_None;
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
217 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
218 Py_INCREF(ob);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
219 res->ob_item[i] = ob;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
222 Py_DECREF(arg);
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 return (PyObject*) res;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 make_tuple(PyStructSequence *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 return structseq_slice(obj, 0, VISIBLE_SIZE(obj));
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 structseq_repr(PyStructSequence *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 {
44671
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
235 /* buffer and type size were chosen well considered. */
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
236 #define REPR_BUFFER_SIZE 512
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
237 #define TYPE_MAXSIZE 100
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
238
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
239 PyObject *tup;
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
240 PyTypeObject *typ = Py_TYPE(obj);
44671
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
241 int i, removelast = 0;
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
242 Py_ssize_t len;
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
243 char buf[REPR_BUFFER_SIZE];
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
244 char *endofbuf, *pbuf = buf;
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
245
44671
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
246 /* pointer to end of writeable buffer; safes space for "...)\0" */
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
247 endofbuf= &buf[REPR_BUFFER_SIZE-5];
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
248
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
249 if ((tup = make_tuple(obj)) == NULL) {
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
250 return NULL;
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
251 }
44671
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
252
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
253 /* "typename(", limited to TYPE_MAXSIZE */
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
254 len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE :
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
255 strlen(typ->tp_name);
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
256 strncpy(pbuf, typ->tp_name, len);
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
257 pbuf += len;
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
258 *pbuf++ = '(';
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
259
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
260 for (i=0; i < VISIBLE_SIZE(obj); i++) {
44671
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
261 PyObject *val, *repr;
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
262 char *cname, *crepr;
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
263
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
264 cname = typ->tp_members[i].name;
44671
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
265
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
266 val = PyTuple_GetItem(tup, i);
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
267 if (cname == NULL || val == NULL) {
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
268 return NULL;
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
269 }
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
270 repr = PyObject_Repr(val);
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
271 if (repr == NULL) {
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
272 Py_DECREF(tup);
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
273 return NULL;
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
274 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
275 crepr = PyString_AsString(repr);
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
276 if (crepr == NULL) {
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
277 Py_DECREF(tup);
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
278 Py_DECREF(repr);
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
279 return NULL;
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
280 }
44671
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
281
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
282 /* + 3: keep space for "=" and ", " */
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
283 len = strlen(cname) + strlen(crepr) + 3;
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
284 if ((pbuf+len) <= endofbuf) {
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
285 strcpy(pbuf, cname);
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
286 pbuf += strlen(cname);
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
287 *pbuf++ = '=';
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
288 strcpy(pbuf, crepr);
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
289 pbuf += strlen(crepr);
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
290 *pbuf++ = ',';
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
291 *pbuf++ = ' ';
44671
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
292 removelast = 1;
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
293 Py_DECREF(repr);
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
294 }
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
295 else {
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
296 strcpy(pbuf, "...");
44671
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
297 pbuf += 3;
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
298 removelast = 0;
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
299 Py_DECREF(repr);
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
300 break;
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
301 }
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
302 }
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 Py_DECREF(tup);
44671
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
304 if (removelast) {
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
305 /* overwrite last ", " */
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
306 pbuf-=2;
33f6709075e8 Added more comments to the new structseq repr code and implemented several of Neal's suggestions.
Christian Heimes <christian@cheimes.de>
parents: 44668
diff changeset
307 }
44668
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
308 *pbuf++ = ')';
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
309 *pbuf = '\0';
7e41e1c46b57 I missed the most important file
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
310
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
311 return PyString_FromString(buf);
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
313
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 structseq_concat(PyStructSequence *obj, PyObject *b)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 PyObject *tup, *result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 tup = make_tuple(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 result = PySequence_Concat(tup, b);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
323
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
325 structseq_repeat(PyStructSequence *obj, Py_ssize_t n)
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 PyObject *tup, *result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 tup = make_tuple(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 result = PySequence_Repeat(tup, n);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
333
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 static int
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 structseq_contains(PyStructSequence *obj, PyObject *o)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 PyObject *tup;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 int result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 tup = make_tuple(obj);
39445
e11caad7e029 Klocwork made another run and found a bunch more problems.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37745
diff changeset
340 if (!tup)
e11caad7e029 Klocwork made another run and found a bunch more problems.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37745
diff changeset
341 return -1;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 result = PySequence_Contains(tup, o);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
346
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 static long
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 structseq_hash(PyObject *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 PyObject *tup;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 long result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 tup = make_tuple((PyStructSequence*) obj);
39445
e11caad7e029 Klocwork made another run and found a bunch more problems.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37745
diff changeset
353 if (!tup)
e11caad7e029 Klocwork made another run and found a bunch more problems.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37745
diff changeset
354 return -1;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 result = PyObject_Hash(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
359
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 structseq_richcompare(PyObject *obj, PyObject *o2, int op)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 PyObject *tup, *result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 tup = make_tuple((PyStructSequence*) obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 result = PyObject_RichCompare(tup, o2, op);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
369
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
370 static PyObject *
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
371 structseq_reduce(PyStructSequence* self)
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
372 {
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
373 PyObject* tup;
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
374 PyObject* dict;
22168
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
375 PyObject* result;
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36511
diff changeset
376 Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields;
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
377 int i;
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
378
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
379 n_fields = REAL_SIZE(self);
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
380 n_visible_fields = VISIBLE_SIZE(self);
25808
fdca87c4234e Add n_unnamed_fields into the type.
Martin v. Löwis <martin@v.loewis.de>
parents: 25806
diff changeset
381 n_unnamed_fields = UNNAMED_FIELDS(self);
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
382 tup = PyTuple_New(n_visible_fields);
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
383 if (!tup) {
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
384 return NULL;
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
385 }
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
386
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
387 dict = PyDict_New();
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
388 if (!dict) {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
389 Py_DECREF(tup);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
390 return NULL;
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
391 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
392
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
393 for (i = 0; i < n_visible_fields; i++) {
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
394 Py_INCREF(self->ob_item[i]);
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
395 PyTuple_SET_ITEM(tup, i, self->ob_item[i]);
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
396 }
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
397
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
398 for (; i < n_fields; i++) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42951
diff changeset
399 char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name;
25808
fdca87c4234e Add n_unnamed_fields into the type.
Martin v. Löwis <martin@v.loewis.de>
parents: 25806
diff changeset
400 PyDict_SetItemString(dict, n,
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
401 self->ob_item[i]);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
402 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
403
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42951
diff changeset
404 result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict);
22168
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
405
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
406 Py_DECREF(tup);
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
407 Py_DECREF(dict);
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
408
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
409 return result;
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
410 }
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
411
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 static PySequenceMethods structseq_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
413 (lenfunc)structseq_length,
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 (binaryfunc)structseq_concat, /* sq_concat */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
415 (ssizeargfunc)structseq_repeat, /* sq_repeat */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
416 (ssizeargfunc)structseq_item, /* sq_item */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
417 (ssizessizeargfunc)structseq_slice, /* sq_slice */
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 0, /* sq_ass_item */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 0, /* sq_ass_slice */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 (objobjproc)structseq_contains, /* sq_contains */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 };
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
422
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
423 static PyMappingMethods structseq_as_mapping = {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
424 (lenfunc)structseq_length,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
425 (binaryfunc)structseq_subscript,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
426 };
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
427
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
428 static PyMethodDef structseq_methods[] = {
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
429 {"__reduce__", (PyCFunction)structseq_reduce,
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
430 METH_NOARGS, NULL},
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
431 {NULL, NULL}
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
432 };
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
433
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 static PyTypeObject _struct_sequence_template = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 39445
diff changeset
435 PyVarObject_HEAD_INIT(&PyType_Type, 0)
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 NULL, /* tp_name */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 0, /* tp_basicsize */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 0, /* tp_itemsize */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 (destructor)structseq_dealloc, /* tp_dealloc */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 0, /* tp_print */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 0, /* tp_getattr */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 0, /* tp_setattr */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 0, /* tp_compare */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 (reprfunc)structseq_repr, /* tp_repr */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 0, /* tp_as_number */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 &structseq_as_sequence, /* tp_as_sequence */
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
447 &structseq_as_mapping, /* tp_as_mapping */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36675
diff changeset
448 structseq_hash, /* tp_hash */
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 0, /* tp_call */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 0, /* tp_str */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 0, /* tp_getattro */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 0, /* tp_setattro */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 0, /* tp_as_buffer */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 Py_TPFLAGS_DEFAULT, /* tp_flags */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 NULL, /* tp_doc */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 0, /* tp_traverse */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 0, /* tp_clear */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 structseq_richcompare, /* tp_richcompare */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 0, /* tp_weaklistoffset */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 0, /* tp_iter */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 0, /* tp_iternext */
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
462 structseq_methods, /* tp_methods */
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 NULL, /* tp_members */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 0, /* tp_getset */
44687
8689f46d25d6 Temporarily revert 59967 until GC can be added.
Raymond Hettinger <python@rcn.com>
parents: 44684
diff changeset
465 0, /* tp_base */
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 0, /* tp_dict */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 0, /* tp_descr_get */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 0, /* tp_descr_set */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 0, /* tp_dictoffset */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 0, /* tp_init */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 0, /* tp_alloc */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 structseq_new, /* tp_new */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 };
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
474
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 void
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 PyObject *dict;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 PyMemberDef* members;
25806
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
480 int n_members, n_unnamed_members, i, k;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
481
37745
353fe06dab5b Unlink the structseq type from the global list of
Martin v. Löwis <martin@v.loewis.de>
parents: 37304
diff changeset
482 #ifdef Py_TRACE_REFS
353fe06dab5b Unlink the structseq type from the global list of
Martin v. Löwis <martin@v.loewis.de>
parents: 37304
diff changeset
483 /* if the type object was chained, unchain it first
353fe06dab5b Unlink the structseq type from the global list of
Martin v. Löwis <martin@v.loewis.de>
parents: 37304
diff changeset
484 before overwriting its storage */
353fe06dab5b Unlink the structseq type from the global list of
Martin v. Löwis <martin@v.loewis.de>
parents: 37304
diff changeset
485 if (type->_ob_next) {
353fe06dab5b Unlink the structseq type from the global list of
Martin v. Löwis <martin@v.loewis.de>
parents: 37304
diff changeset
486 _Py_ForgetReference((PyObject*)type);
353fe06dab5b Unlink the structseq type from the global list of
Martin v. Löwis <martin@v.loewis.de>
parents: 37304
diff changeset
487 }
353fe06dab5b Unlink the structseq type from the global list of
Martin v. Löwis <martin@v.loewis.de>
parents: 37304
diff changeset
488 #endif
353fe06dab5b Unlink the structseq type from the global list of
Martin v. Löwis <martin@v.loewis.de>
parents: 37304
diff changeset
489
25806
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
490 n_unnamed_members = 0;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 for (i = 0; desc->fields[i].name != NULL; ++i)
25808
fdca87c4234e Add n_unnamed_fields into the type.
Martin v. Löwis <martin@v.loewis.de>
parents: 25806
diff changeset
492 if (desc->fields[i].name == PyStructSequence_UnnamedField)
25806
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
493 n_unnamed_members++;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 n_members = i;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
495
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 memcpy(type, &_struct_sequence_template, sizeof(PyTypeObject));
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 type->tp_name = desc->name;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 type->tp_doc = desc->doc;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 type->tp_basicsize = sizeof(PyStructSequence)+
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 sizeof(PyObject*)*(n_members-1);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 type->tp_itemsize = 0;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
502
25806
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
503 members = PyMem_NEW(PyMemberDef, n_members-n_unnamed_members+1);
26708
3593f2b91d7d SF # 654974, fix unchecked return values in structseq
Neal Norwitz <nnorwitz@gmail.com>
parents: 25808
diff changeset
504 if (members == NULL)
3593f2b91d7d SF # 654974, fix unchecked return values in structseq
Neal Norwitz <nnorwitz@gmail.com>
parents: 25808
diff changeset
505 return;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
506
25806
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
507 for (i = k = 0; i < n_members; ++i) {
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
508 if (desc->fields[i].name == PyStructSequence_UnnamedField)
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
509 continue;
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
510 members[k].name = desc->fields[i].name;
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
511 members[k].type = T_OBJECT;
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
512 members[k].offset = offsetof(PyStructSequence, ob_item)
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 + i * sizeof(PyObject*);
25806
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
514 members[k].flags = READONLY;
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
515 members[k].doc = desc->fields[i].doc;
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
516 k++;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 }
25806
0bbea4dcd9be Add PyStructSequence_UnnamedField. Add stat_float_times.
Martin v. Löwis <martin@v.loewis.de>
parents: 22978
diff changeset
518 members[k].name = NULL;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
519
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 type->tp_members = members;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
521
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 if (PyType_Ready(type) < 0)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 return;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 Py_INCREF(type);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
525
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 dict = type->tp_dict;
48847
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
527 #define SET_DICT_FROM_INT(key, value) \
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
528 do { \
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
529 PyObject *v = PyInt_FromLong((long) value); \
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
530 if (v != NULL) { \
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
531 PyDict_SetItemString(dict, key, v); \
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
532 Py_DECREF(v); \
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
533 } \
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
534 } while (0)
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
535
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
536 SET_DICT_FROM_INT(visible_length_key, desc->n_in_sequence);
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
537 SET_DICT_FROM_INT(real_length_key, n_members);
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
538 SET_DICT_FROM_INT(unnamed_fields_key, n_unnamed_members);
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 }