annotate Objects/structseq.c @ 21283:d4907cdcc299 legacy-trunk

Use an immutable tuple for __slots instead of a mutable list Bump version number
author Andrew M. Kuchling <amk@amk.ca>
date Fri, 07 Dec 2001 14:22:13 +0000
parents 94f5a9fa3583
children 1a5110b4bd72
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";
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #define VISIBLE_SIZE(op) ((op)->ob_size)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 #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
13 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
14
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 #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
16 PyDict_GetItemString((tp)->tp_dict, real_length_key))
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 #define REAL_SIZE(op) REAL_SIZE_TP((op)->ob_type)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
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 PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 PyStructSequence_New(PyTypeObject *type)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 PyStructSequence *obj;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 obj = PyObject_New(PyStructSequence, type);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 obj->ob_size = VISIBLE_SIZE_TP(type);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 return (PyObject*) obj;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 static void
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 structseq_dealloc(PyStructSequence *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 int i, size;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 size = REAL_SIZE(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 for (i = 0; i < size; ++i) {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 Py_XDECREF(obj->ob_item[i]);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 PyObject_FREE(obj);
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
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 static int
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 structseq_length(PyStructSequence *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 return VISIBLE_SIZE(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 static PyObject*
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 structseq_item(PyStructSequence *obj, int i)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 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
53 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
54 return NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 Py_INCREF(obj->ob_item[i]);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 return obj->ob_item[i];
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*
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 structseq_slice(PyStructSequence *obj, int low, int high)
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 PyTupleObject *np;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 int i;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 if (low < 0)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 low = 0;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 if (high > VISIBLE_SIZE(obj))
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 high = VISIBLE_SIZE(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 if (high < low)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 high = low;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 np = (PyTupleObject *)PyTuple_New(high-low);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 if (np == NULL)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 return NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 for(i = low; i < high; ++i) {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 PyObject *v = obj->ob_item[i];
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 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
78 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
79 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 return (PyObject *) np;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 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
85 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 PyObject *arg = NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 PyStructSequence *res = NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 int len, required_len, i;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 static char *kwlist[] = {"sequence", 0};
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 static char msgbuf[128];
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:structseq",
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 kwlist, &arg))
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 return NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 if (!PySequence_Check(arg)) {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 PyErr_SetString(PyExc_TypeError,
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 "constructor requires a sequence");
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 return NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 len = PySequence_Length(arg);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 required_len = REAL_SIZE_TP(type);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 if (len != required_len) {
21081
94f5a9fa3583 structseq_new(): Conversion of sprintf() to PyOS_snprintf() for buffer
Barry Warsaw <barry@python.org>
parents: 20780
diff changeset
105 PyOS_snprintf(
94f5a9fa3583 structseq_new(): Conversion of sprintf() to PyOS_snprintf() for buffer
Barry Warsaw <barry@python.org>
parents: 20780
diff changeset
106 msgbuf, sizeof(msgbuf),
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 "constructor takes exactly %d arguments (%d given)",
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 required_len,
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 len);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 PyErr_SetString(PyExc_TypeError, msgbuf);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 return NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 res = (PyStructSequence*) PyStructSequence_New(type);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 for (i = 0; i < len; ++i) {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 /* INCREF???? XXXX */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 res->ob_item[i] = PySequence_GetItem(arg, i);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 return (PyObject*) res;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 make_tuple(PyStructSequence *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 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
127 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 structseq_repr(PyStructSequence *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 PyObject *tup, *str;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 tup = make_tuple(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 str = PyObject_Repr(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 return str;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 structseq_concat(PyStructSequence *obj, PyObject *b)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 PyObject *tup, *result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 tup = make_tuple(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 result = PySequence_Concat(tup, b);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 structseq_repeat(PyStructSequence *obj, int n)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 PyObject *tup, *result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 tup = make_tuple(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 result = PySequence_Repeat(tup, n);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 static int
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 structseq_contains(PyStructSequence *obj, PyObject *o)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 PyObject *tup;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 int result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 tup = make_tuple(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 result = PySequence_Contains(tup, o);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 return result;
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
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 static long
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 structseq_hash(PyObject *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 PyObject *tup;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 long result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 tup = make_tuple((PyStructSequence*) obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 result = PyObject_Hash(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 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
183 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 PyObject *tup, *result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 tup = make_tuple((PyStructSequence*) obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 result = PyObject_RichCompare(tup, o2, op);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 static PySequenceMethods structseq_as_sequence = {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 (inquiry)structseq_length,
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 (binaryfunc)structseq_concat, /* sq_concat */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 (intargfunc)structseq_repeat, /* sq_repeat */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 (intargfunc)structseq_item, /* sq_item */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 (intintargfunc)structseq_slice, /* sq_slice */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 0, /* sq_ass_item */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 0, /* sq_ass_slice */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 (objobjproc)structseq_contains, /* sq_contains */
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 static PyTypeObject _struct_sequence_template = {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 PyObject_HEAD_INIT(&PyType_Type)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 0, /* ob_size */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 NULL, /* tp_name */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 0, /* tp_basicsize */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 0, /* tp_itemsize */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 (destructor)structseq_dealloc, /* tp_dealloc */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 0, /* tp_print */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 0, /* tp_getattr */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 0, /* tp_setattr */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 0, /* tp_compare */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 (reprfunc)structseq_repr, /* tp_repr */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 0, /* tp_as_number */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 &structseq_as_sequence, /* tp_as_sequence */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 0, /* tp_as_mapping */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 (hashfunc)structseq_hash, /* tp_hash */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 0, /* tp_call */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 0, /* tp_str */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 0, /* tp_getattro */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 0, /* tp_setattro */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 0, /* tp_as_buffer */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 Py_TPFLAGS_DEFAULT, /* tp_flags */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 NULL, /* tp_doc */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 0, /* tp_traverse */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 0, /* tp_clear */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 structseq_richcompare, /* tp_richcompare */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 0, /* tp_weaklistoffset */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 0, /* tp_iter */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 0, /* tp_iternext */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 0, /* tp_methods */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 NULL, /* tp_members */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 0, /* tp_getset */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 0, /* tp_base */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 0, /* tp_dict */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 0, /* tp_descr_get */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 0, /* tp_descr_set */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 0, /* tp_dictoffset */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 0, /* tp_init */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 0, /* tp_alloc */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 structseq_new, /* tp_new */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 };
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 void
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 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
246 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 PyObject *dict;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 PyMemberDef* members;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 int n_members, i;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 for (i = 0; desc->fields[i].name != NULL; ++i)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 ;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 n_members = i;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
254
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 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
256 type->tp_name = desc->name;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 type->tp_doc = desc->doc;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 type->tp_basicsize = sizeof(PyStructSequence)+
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 sizeof(PyObject*)*(n_members-1);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 type->tp_itemsize = 0;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 members = PyMem_NEW(PyMemberDef, n_members+1);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
263
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 for (i = 0; i < n_members; ++i) {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 members[i].name = desc->fields[i].name;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 members[i].type = T_OBJECT;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 members[i].offset = offsetof(PyStructSequence, ob_item)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 + i * sizeof(PyObject*);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 members[i].flags = READONLY;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 members[i].doc = desc->fields[i].doc;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 members[n_members].name = NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 type->tp_members = members;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
275
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 if (PyType_Ready(type) < 0)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 return;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 Py_INCREF(type);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
279
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 dict = type->tp_dict;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 PyDict_SetItemString(dict, visible_length_key,
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 PyInt_FromLong((long) desc->n_in_sequence));
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 PyDict_SetItemString(dict, real_length_key,
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 PyInt_FromLong((long) n_members));
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 }