annotate Objects/structseq.c @ 68235:61b0263d6881 2.2

Close 2.2 branch.
author Georg Brandl <georg@python.org>
date Sat, 05 Mar 2011 14:55:23 +0100
parents beb2008a3c32
children
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;
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
87 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
88 PyObject *ob;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 PyStructSequence *res = 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
90 int len, min_len, 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
91 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
92
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
93 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
94 kwlist, &arg, &dict))
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 return NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
97 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
98
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
99 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
100 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
101 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
102
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
103 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
104 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
105 "%.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
106 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
107 Py_DECREF(arg);
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 return NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
111 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
112 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
113 max_len = REAL_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
114
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
115 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
116 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
117 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
118 "%.500s() takes an at least %d-sequence (%d-sequence given)",
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
119 type->tp_name, min_len, len);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
120 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
121 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
122 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
123
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
124 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
125 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
126 "%.500s() takes an at most %d-sequence (%d-sequence given)",
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
127 type->tp_name, max_len, len);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
128 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
129 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
130 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
131 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
132 else {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
133 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
134 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
135 "%.500s() takes a %d-sequence (%d-sequence given)",
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
136 type->tp_name, min_len, len);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
137 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
138 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
139 }
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 }
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 res = (PyStructSequence*) PyStructSequence_New(type);
27608
beb2008a3c32 backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 22168
diff changeset
143 if (res == NULL) {
beb2008a3c32 backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 22168
diff changeset
144 return NULL;
beb2008a3c32 backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 22168
diff changeset
145 }
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 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
147 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
148 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
149 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
150 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
151 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
152 if (dict && (ob = PyDict_GetItemString(
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
153 dict, type->tp_members[i].name))) {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
154 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
155 else {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
156 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
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 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
159 res->ob_item[i] = ob;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
162 Py_DECREF(arg);
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 return (PyObject*) res;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 make_tuple(PyStructSequence *obj)
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 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
170 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 structseq_repr(PyStructSequence *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 PyObject *tup, *str;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 tup = make_tuple(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 str = PyObject_Repr(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 return str;
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
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 structseq_concat(PyStructSequence *obj, PyObject *b)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 PyObject *tup, *result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 tup = make_tuple(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 result = PySequence_Concat(tup, b);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 return result;
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
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 structseq_repeat(PyStructSequence *obj, int n)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 PyObject *tup, *result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 tup = make_tuple(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 result = PySequence_Repeat(tup, n);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 return result;
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 int
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 structseq_contains(PyStructSequence *obj, PyObject *o)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 PyObject *tup;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 int result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 tup = make_tuple(obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 result = PySequence_Contains(tup, o);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
212
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 static long
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 structseq_hash(PyObject *obj)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 PyObject *tup;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 long result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 tup = make_tuple((PyStructSequence*) obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 result = PyObject_Hash(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 static PyObject *
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 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
226 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 PyObject *tup, *result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 tup = make_tuple((PyStructSequence*) obj);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 result = PyObject_RichCompare(tup, o2, op);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 Py_DECREF(tup);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 return result;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
233
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
234 static PyObject *
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
235 structseq_reduce(PyStructSequence* self)
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
236 {
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
237 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
238 PyObject* dict;
22168
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
239 PyObject* result;
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
240 long n_fields, n_visible_fields;
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
241 int i;
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
242
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
243 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
244 n_visible_fields = VISIBLE_SIZE(self);
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
245 tup = PyTuple_New(n_visible_fields);
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
246 if (!tup) {
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
247 return NULL;
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
248 }
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
249
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
250 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
251 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
252 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
253 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
254 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
255
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
256 for (i = 0; i < n_visible_fields; i++) {
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
257 Py_INCREF(self->ob_item[i]);
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
258 PyTuple_SET_ITEM(tup, i, self->ob_item[i]);
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
259 }
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
260
22166
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
261 for (; i < n_fields; i++) {
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
262 PyDict_SetItemString(dict, self->ob_type->tp_members[i].name,
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
263 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
264 }
837951ecdce9 After some thinking, I decided to move all of this across onto
Michael W. Hudson <mwh@python.net>
parents: 22137
diff changeset
265
22168
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
266 result = Py_BuildValue("(O(OO))", self->ob_type, tup, dict);
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
267
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
268 Py_DECREF(tup);
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
269 Py_DECREF(dict);
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
270
68831ac5495b backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22166
diff changeset
271 return result;
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
272 }
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
273
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 static PySequenceMethods structseq_as_sequence = {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 (inquiry)structseq_length,
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 (binaryfunc)structseq_concat, /* sq_concat */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 (intargfunc)structseq_repeat, /* sq_repeat */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 (intargfunc)structseq_item, /* sq_item */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 (intintargfunc)structseq_slice, /* sq_slice */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 0, /* sq_ass_item */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 0, /* sq_ass_slice */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 (objobjproc)structseq_contains, /* sq_contains */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 };
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
284
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
285 static PyMethodDef structseq_methods[] = {
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
286 {"__reduce__", (PyCFunction)structseq_reduce,
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
287 METH_NOARGS, NULL},
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
288 {NULL, NULL}
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
289 };
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
290
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 static PyTypeObject _struct_sequence_template = {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 PyObject_HEAD_INIT(&PyType_Type)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 0, /* ob_size */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 NULL, /* tp_name */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 0, /* tp_basicsize */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 0, /* tp_itemsize */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 (destructor)structseq_dealloc, /* tp_dealloc */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 0, /* tp_print */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 0, /* tp_getattr */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 0, /* tp_setattr */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 0, /* tp_compare */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 (reprfunc)structseq_repr, /* tp_repr */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 0, /* tp_as_number */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 &structseq_as_sequence, /* tp_as_sequence */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 0, /* tp_as_mapping */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 (hashfunc)structseq_hash, /* tp_hash */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 0, /* tp_call */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 0, /* tp_str */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 0, /* tp_getattro */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 0, /* tp_setattro */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 0, /* tp_as_buffer */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 Py_TPFLAGS_DEFAULT, /* tp_flags */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 NULL, /* tp_doc */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 0, /* tp_traverse */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 0, /* tp_clear */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 structseq_richcompare, /* tp_richcompare */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 0, /* tp_weaklistoffset */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 0, /* tp_iter */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 0, /* tp_iternext */
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
320 structseq_methods, /* tp_methods */
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 NULL, /* tp_members */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 0, /* tp_getset */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 0, /* tp_base */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 0, /* tp_dict */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 0, /* tp_descr_get */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 0, /* tp_descr_set */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 0, /* tp_dictoffset */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 0, /* tp_init */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 0, /* tp_alloc */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 structseq_new, /* tp_new */
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 };
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 void
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 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
335 {
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 PyObject *dict;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 PyMemberDef* members;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 int n_members, i;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
339
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 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
341 ;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 n_members = i;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
343
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 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
345 type->tp_name = desc->name;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 type->tp_doc = desc->doc;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 type->tp_basicsize = sizeof(PyStructSequence)+
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 sizeof(PyObject*)*(n_members-1);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 type->tp_itemsize = 0;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
350
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 members = PyMem_NEW(PyMemberDef, n_members+1);
27608
beb2008a3c32 backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 22168
diff changeset
352 if (members == NULL)
beb2008a3c32 backport:
Neal Norwitz <nnorwitz@gmail.com>
parents: 22168
diff changeset
353 return;
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 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
356 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
357 members[i].type = T_OBJECT;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 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
359 + i * sizeof(PyObject*);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 members[i].flags = READONLY;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 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
362 }
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 members[n_members].name = NULL;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
364
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 type->tp_members = members;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
366
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 if (PyType_Ready(type) < 0)
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 return;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 Py_INCREF(type);
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
370
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 dict = type->tp_dict;
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 PyDict_SetItemString(dict, visible_length_key,
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 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
374 PyDict_SetItemString(dict, real_length_key,
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 PyInt_FromLong((long) n_members));
22137
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
376 PyDict_SetItemString(dict, "__safe_for_unpickling__",
1a5110b4bd72 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21081
diff changeset
377 PyInt_FromLong(1));
20540
ad2662095d77 Missing file structseq.c for SF patch #462296
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 }