/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
/* Copyright (C) 2009, 2010 Canonical Ltd
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
2
 * 
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation; either version 2 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program; if not, write to the Free Software
15
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4679.3.5 by John Arbash Meinel
Have a pure-python implementation that works as tuples of tuples of strings,
16
 */
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
17
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
18
/* Must be defined before importing _static_tuple_c.h so that we get the right
19
 * linkage.
20
 */
21
#define STATIC_TUPLE_MODULE
22
4736.1.1 by John Arbash Meinel
Py_ssize_t and its associated function typedefs are not available w/ python 2.4
23
#include <Python.h>
24
#include "python-compat.h"
25
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
26
#include "_static_tuple_c.h"
27
#include "_export_c_api.h"
4679.5.11 by John Arbash Meinel
add a hack-around to handle differences between pyrex 0.9.6 and 0.9.8
28
4679.3.76 by John Arbash Meinel
Rename StaticTupleInterner => SimpleSet.
29
#include "_simple_set_pyx_api.h"
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
30
31
#if defined(__GNUC__)
32
#   define inline __inline__
33
#elif defined(_MSC_VER)
34
#   define inline __inline
35
#else
36
#   define inline
37
#endif
38
39
4679.3.44 by John Arbash Meinel
Special case the empty tuple as a singleton.
40
/* The one and only StaticTuple with no values */
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
41
static StaticTuple *_empty_tuple = NULL;
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
42
static PyObject *_interned_tuples = NULL;
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
43
44
4679.3.33 by John Arbash Meinel
Change Key away from being a PyVarObject.
45
static inline int
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
46
_StaticTuple_is_interned(StaticTuple *self)
4679.3.35 by John Arbash Meinel
Work on making intern() not generate immortal Key objects.
47
{
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
48
    return self->flags & STATIC_TUPLE_INTERNED_FLAG;
4679.3.35 by John Arbash Meinel
Work on making intern() not generate immortal Key objects.
49
}
50
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
51
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
52
53
static PyObject *
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
54
StaticTuple_as_tuple(StaticTuple *self)
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
55
{
56
    PyObject *tpl = NULL, *obj = NULL;
4679.3.33 by John Arbash Meinel
Change Key away from being a PyVarObject.
57
    int i, len;
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
58
4679.3.37 by John Arbash Meinel
Change the Key header a bit. It is simpler if you use unsigned char
59
    len = self->size;
4679.3.33 by John Arbash Meinel
Change Key away from being a PyVarObject.
60
    tpl = PyTuple_New(len);
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
61
    if (!tpl) {
62
        /* Malloc failure */
63
        return NULL;
64
    }
4679.3.33 by John Arbash Meinel
Change Key away from being a PyVarObject.
65
    for (i = 0; i < len; ++i) {
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
66
        obj = (PyObject *)self->items[i];
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
67
        Py_INCREF(obj);
68
        PyTuple_SET_ITEM(tpl, i, obj);
69
    }
70
    return tpl;
71
}
72
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
73
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
74
static char StaticTuple_as_tuple_doc[] = "as_tuple() => tuple";
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
75
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
76
static StaticTuple *
77
StaticTuple_Intern(StaticTuple *self)
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
78
{
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
79
    PyObject *canonical_tuple = NULL;
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
80
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
81
    if (_interned_tuples == NULL || _StaticTuple_is_interned(self)) {
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
82
        Py_INCREF(self);
83
        return self;
84
    }
4679.3.76 by John Arbash Meinel
Rename StaticTupleInterner => SimpleSet.
85
    /* SimpleSet_Add returns whatever object is present at self
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
86
     * or the new object if it needs to add it.
87
     */
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
88
    canonical_tuple = SimpleSet_Add(_interned_tuples, (PyObject *)self);
89
    if (!canonical_tuple) {
90
        // Some sort of exception, propogate it.
91
        return NULL;
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
92
    }
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
93
    if (canonical_tuple != (PyObject *)self) {
94
        // There was already a tuple with that value
95
        return (StaticTuple *)canonical_tuple;
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
96
    }
97
    self->flags |= STATIC_TUPLE_INTERNED_FLAG;
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
98
    // The two references in the dict do not count, so that the StaticTuple
99
    // object does not become immortal just because it was interned.
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
100
    Py_REFCNT(self) -= 1;
101
    return self;
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
102
}
103
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
104
static char StaticTuple_Intern_doc[] = "intern() => unique StaticTuple\n"
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
105
    "Return a 'canonical' StaticTuple object.\n"
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
106
    "Similar to intern() for strings, this makes sure there\n"
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
107
    "is only one StaticTuple object for a given value\n."
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
108
    "Common usage is:\n"
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
109
    "  key = StaticTuple('foo', 'bar').intern()\n";
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
110
111
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
112
static void
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
113
StaticTuple_dealloc(StaticTuple *self)
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
114
{
4679.3.33 by John Arbash Meinel
Change Key away from being a PyVarObject.
115
    int i, len;
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
116
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
117
    if (_StaticTuple_is_interned(self)) {
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
118
        /* revive dead object temporarily for Discard */
119
        Py_REFCNT(self) = 2;
4679.3.76 by John Arbash Meinel
Rename StaticTupleInterner => SimpleSet.
120
        if (SimpleSet_Discard(_interned_tuples, (PyObject*)self) != 1)
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
121
            Py_FatalError("deletion of interned StaticTuple failed");
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
122
        self->flags &= ~STATIC_TUPLE_INTERNED_FLAG;
4679.3.35 by John Arbash Meinel
Work on making intern() not generate immortal Key objects.
123
    }
4679.3.37 by John Arbash Meinel
Change the Key header a bit. It is simpler if you use unsigned char
124
    len = self->size;
4679.3.33 by John Arbash Meinel
Change Key away from being a PyVarObject.
125
    for (i = 0; i < len; ++i) {
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
126
        Py_XDECREF(self->items[i]);
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
127
    }
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
128
    Py_TYPE(self)->tp_free((PyObject *)self);
129
}
130
131
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
132
/* Similar to PyTuple_New() */
133
static StaticTuple *
134
StaticTuple_New(Py_ssize_t size)
135
{
136
    StaticTuple *stuple;
137
4759.2.7 by John Arbash Meinel
Re-implement StaticTuple_add() inline, rather that thunking over to tuples.
138
    if (size < 0 || size > 255) {
139
        /* Too big or too small */
140
        PyErr_SetString(PyExc_ValueError, "StaticTuple(...)"
141
            " takes from 0 to 255 items");
142
        return NULL;
143
    }
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
144
    if (size == 0 && _empty_tuple != NULL) {
145
        Py_INCREF(_empty_tuple);
146
        return _empty_tuple;
147
    }
148
    /* Note that we use PyObject_NewVar because we want to allocate a variable
149
     * width entry. However we *aren't* truly a PyVarObject because we don't
150
     * use a long for ob_size. Instead we use a plain 'size' that is an int,
151
     * and will be overloaded with flags in the future.
152
     * As such we do the alloc, and then have to clean up anything it does
153
     * incorrectly.
154
     */
155
    stuple = PyObject_NewVar(StaticTuple, &StaticTuple_Type, size);
156
    if (stuple == NULL) {
157
        return NULL;
158
    }
159
    stuple->size = size;
160
    stuple->flags = 0;
161
    stuple->_unused0 = 0;
162
    stuple->_unused1 = 0;
163
    if (size > 0) {
164
        memset(stuple->items, 0, sizeof(PyObject *) * size);
165
    }
166
#if STATIC_TUPLE_HAS_HASH
167
    stuple->hash = -1;
168
#endif
169
    return stuple;
170
}
171
172
4739.4.1 by John Arbash Meinel
Implement StaticTuple.from_sequence()
173
static StaticTuple *
174
StaticTuple_FromSequence(PyObject *sequence)
175
{
4771.2.2 by John Arbash Meinel
Clean up the C code a bit, using a goto.
176
    StaticTuple *new = NULL;
4771.2.1 by Matt Nordhoff
_static_tuple_c.StaticTuple.from_sequence() now supports arbitrary iterables (by converting them to tuples first).
177
    PyObject *as_tuple = NULL;
4739.4.1 by John Arbash Meinel
Implement StaticTuple.from_sequence()
178
    PyObject *item;
179
    Py_ssize_t i, size;
180
181
    if (StaticTuple_CheckExact(sequence)) {
182
        Py_INCREF(sequence);
183
        return (StaticTuple *)sequence;
184
    }
185
    if (!PySequence_Check(sequence)) {
4771.2.1 by Matt Nordhoff
_static_tuple_c.StaticTuple.from_sequence() now supports arbitrary iterables (by converting them to tuples first).
186
        as_tuple = PySequence_Tuple(sequence);
4771.2.2 by John Arbash Meinel
Clean up the C code a bit, using a goto.
187
        if (as_tuple == NULL)
188
            goto done;
4771.2.1 by Matt Nordhoff
_static_tuple_c.StaticTuple.from_sequence() now supports arbitrary iterables (by converting them to tuples first).
189
        sequence = as_tuple;
4739.4.1 by John Arbash Meinel
Implement StaticTuple.from_sequence()
190
    }
191
    size = PySequence_Size(sequence);
4771.2.1 by Matt Nordhoff
_static_tuple_c.StaticTuple.from_sequence() now supports arbitrary iterables (by converting them to tuples first).
192
    if (size == -1) {
4771.2.2 by John Arbash Meinel
Clean up the C code a bit, using a goto.
193
        goto done;
4771.2.1 by Matt Nordhoff
_static_tuple_c.StaticTuple.from_sequence() now supports arbitrary iterables (by converting them to tuples first).
194
    }
4739.4.1 by John Arbash Meinel
Implement StaticTuple.from_sequence()
195
    new = StaticTuple_New(size);
196
    if (new == NULL) {
4771.2.2 by John Arbash Meinel
Clean up the C code a bit, using a goto.
197
        goto done;
4739.4.1 by John Arbash Meinel
Implement StaticTuple.from_sequence()
198
    }
199
    for (i = 0; i < size; ++i) {
200
        // This returns a new reference, which we then 'steal' with 
201
        // StaticTuple_SET_ITEM
202
        item = PySequence_GetItem(sequence, i);
203
        if (item == NULL) {
204
            Py_DECREF(new);
4771.2.2 by John Arbash Meinel
Clean up the C code a bit, using a goto.
205
            new = NULL;
206
            goto done;
4739.4.1 by John Arbash Meinel
Implement StaticTuple.from_sequence()
207
        }
208
        StaticTuple_SET_ITEM(new, i, item);
209
    }
4771.2.2 by John Arbash Meinel
Clean up the C code a bit, using a goto.
210
done:
4771.2.1 by Matt Nordhoff
_static_tuple_c.StaticTuple.from_sequence() now supports arbitrary iterables (by converting them to tuples first).
211
    Py_XDECREF(as_tuple);
4739.4.1 by John Arbash Meinel
Implement StaticTuple.from_sequence()
212
    return (StaticTuple *)new;
213
}
214
215
static StaticTuple *
216
StaticTuple_from_sequence(PyObject *self, PyObject *args, PyObject *kwargs)
217
{
218
    PyObject *sequence;
219
    if (!PyArg_ParseTuple(args, "O", &sequence))
220
        return NULL;
221
    return StaticTuple_FromSequence(sequence);
222
}
223
224
4759.2.6 by John Arbash Meinel
factor out the check of each internal item.
225
/* Check that all items we point to are 'valid' */
226
static int
227
StaticTuple_check_items(StaticTuple *self)
228
{
229
    int i;
230
    PyObject *obj;
231
232
    for (i = 0; i < self->size; ++i) {
233
        obj = self->items[i];
234
        if (obj == NULL) {
235
            PyErr_SetString(PyExc_RuntimeError, "StaticTuple(...)"
236
                " should not have a NULL entry.");
237
            return 0;
238
        }
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
239
        if (PyBytes_CheckExact(obj)
4759.2.9 by John Arbash Meinel
Implement support for lots of types.
240
            || StaticTuple_CheckExact(obj)
241
            || obj == Py_None
242
            || PyBool_Check(obj)
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
243
#if PY_MAJOR_VERSION >= 3
244
#else
4759.2.11 by John Arbash Meinel
Review feedback from Andrew.
245
            || PyInt_CheckExact(obj)
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
246
#endif
4759.2.11 by John Arbash Meinel
Review feedback from Andrew.
247
            || PyLong_CheckExact(obj)
248
            || PyFloat_CheckExact(obj)
249
            || PyUnicode_CheckExact(obj)
4759.2.9 by John Arbash Meinel
Implement support for lots of types.
250
            ) continue;
251
        PyErr_Format(PyExc_TypeError, "StaticTuple(...)"
252
            " requires that all items are one of"
253
            " str, StaticTuple, None, bool, int, long, float, or unicode"
254
            " not %s.", Py_TYPE(obj)->tp_name);
255
        return 0;
4759.2.6 by John Arbash Meinel
factor out the check of each internal item.
256
    }
257
    return 1;
258
}
259
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
260
static PyObject *
4679.5.9 by John Arbash Meinel
NEWS entry for StaticTuple class.
261
StaticTuple_new_constructor(PyTypeObject *type, PyObject *args, PyObject *kwds)
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
262
{
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
263
    StaticTuple *self;
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
264
    PyObject *obj = NULL;
265
    Py_ssize_t i, len = 0;
266
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
267
    if (type != &StaticTuple_Type) {
268
        PyErr_SetString(PyExc_TypeError, "we only support creating StaticTuple");
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
269
        return NULL;
270
    }
271
    if (!PyTuple_CheckExact(args)) {
272
        PyErr_SetString(PyExc_TypeError, "args must be a tuple");
273
        return NULL;
274
    }
275
    len = PyTuple_GET_SIZE(args);
4763.2.3 by Matt Nordhoff
Make StaticTuple_New always raise a ValueError, and StaticTuple_new_constructor always raise a TypeError.
276
    if (len < 0 || len > 255) {
277
        /* Check the length here so we can raise a TypeError instead of
278
         * StaticTuple_New's ValueError.
279
         */
280
        PyErr_SetString(PyExc_TypeError, "StaticTuple(...)"
281
            " takes from 0 to 255 items");
282
        return NULL;
283
    }
4679.3.44 by John Arbash Meinel
Special case the empty tuple as a singleton.
284
    self = (StaticTuple *)StaticTuple_New(len);
4679.3.19 by John Arbash Meinel
implement slicing as a tuple thunk.
285
    if (self == NULL) {
286
        return NULL;
287
    }
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
288
    for (i = 0; i < len; ++i) {
289
        obj = PyTuple_GET_ITEM(args, i);
290
        Py_INCREF(obj);
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
291
        self->items[i] = obj;
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
292
    }
4759.2.6 by John Arbash Meinel
factor out the check of each internal item.
293
    if (!StaticTuple_check_items(self)) {
294
        type->tp_dealloc((PyObject *)self);
295
        return NULL;
296
    }
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
297
    return (PyObject *)self;
298
}
299
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
300
static PyObject *
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
301
StaticTuple_repr(StaticTuple *self)
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
302
{
4679.3.79 by John Arbash Meinel
Change the repr to print out 'StaticTuple'
303
    PyObject *as_tuple, *tuple_repr, *result;
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
304
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
305
    as_tuple = StaticTuple_as_tuple(self);
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
306
    if (as_tuple == NULL) {
307
        return NULL;
308
    }
4679.3.79 by John Arbash Meinel
Change the repr to print out 'StaticTuple'
309
    tuple_repr = PyObject_Repr(as_tuple);
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
310
    Py_DECREF(as_tuple);
4679.3.79 by John Arbash Meinel
Change the repr to print out 'StaticTuple'
311
    if (tuple_repr == NULL) {
312
        return NULL;
313
    }
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
314
#if PY_MAJOR_VERSION >= 3
315
    result = PyUnicode_FromFormat("StaticTuple%U", tuple_repr);
316
#else
4759.2.14 by Matt Nordhoff
Hardcode "StaticTuple" in repr instead of using tp_name, since that includes the module now.
317
    result = PyString_FromFormat("StaticTuple%s",
318
                                 PyString_AsString(tuple_repr));
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
319
#endif
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
320
    return result;
321
}
322
323
static long
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
324
StaticTuple_hash(StaticTuple *self)
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
325
{
4679.3.22 by John Arbash Meinel
It seems that optimizing Keys_hash is not the way to go for 'bzr log' performance.
326
    /* adapted from tuplehash(), is the specific hash value considered
327
     * 'stable'?
328
     */
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
329
    register long x, y;
330
    Py_ssize_t len = self->size;
331
    PyObject **p;
332
    long mult = 1000003L;
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
333
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
334
#if STATIC_TUPLE_HAS_HASH
4679.3.22 by John Arbash Meinel
It seems that optimizing Keys_hash is not the way to go for 'bzr log' performance.
335
    if (self->hash != -1) {
336
        return self->hash;
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
337
    }
4679.3.28 by John Arbash Meinel
Add a KEY_HAS_HASH define.
338
#endif
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
339
    x = 0x345678L;
340
    p = self->items;
4679.3.77 by John Arbash Meinel
Some code cleanup passes.
341
    // TODO: We could set specific flags if we know that, for example, all the
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
342
    //       items are strings. I haven't seen a real-world benefit to that
343
    //       yet, though.
4679.3.77 by John Arbash Meinel
Some code cleanup passes.
344
    while (--len >= 0) {
345
        y = PyObject_Hash(*p++);
346
        if (y == -1) /* failure */
347
            return -1;
348
        x = (x ^ y) * mult;
349
        /* the cast might truncate len; that doesn't change hash stability */
350
        mult += (long)(82520L + len + len);
4679.3.46 by John Arbash Meinel
Play around a bit with changing the hash function.
351
    }
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
352
    x += 97531L;
353
    if (x == -1)
354
        x = -2;
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
355
#if STATIC_TUPLE_HAS_HASH
4679.3.22 by John Arbash Meinel
It seems that optimizing Keys_hash is not the way to go for 'bzr log' performance.
356
    self->hash = x;
4679.3.28 by John Arbash Meinel
Add a KEY_HAS_HASH define.
357
#endif
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
358
    return x;
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
359
}
360
361
static PyObject *
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
362
StaticTuple_richcompare_to_tuple(StaticTuple *v, PyObject *wt, int op)
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
363
{
4679.3.23 by John Arbash Meinel
A bit more testing shows that we are comparing Key to tuples fairly often.
364
    PyObject *vt;
4679.3.19 by John Arbash Meinel
implement slicing as a tuple thunk.
365
    PyObject *result = NULL;
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
366
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
367
    vt = StaticTuple_as_tuple((StaticTuple *)v);
4679.3.23 by John Arbash Meinel
A bit more testing shows that we are comparing Key to tuples fairly often.
368
    if (vt == NULL) {
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
369
        goto done;
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
370
    }
4679.3.23 by John Arbash Meinel
A bit more testing shows that we are comparing Key to tuples fairly often.
371
    if (!PyTuple_Check(wt)) {
372
        PyErr_BadInternalCall();
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
373
        goto done;
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
374
    }
375
    /* Now we have 2 tuples to compare, do it */
376
    result = PyTuple_Type.tp_richcompare(vt, wt, op);
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
377
done:
4679.3.23 by John Arbash Meinel
A bit more testing shows that we are comparing Key to tuples fairly often.
378
    Py_XDECREF(vt);
4679.3.18 by John Arbash Meinel
Copy the hash and richcompare implementations, and add some tests.
379
    return result;
380
}
381
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
382
/** Compare two objects to determine if they are equivalent.
383
 * The basic flow is as follows
384
 *  1) First make sure that both objects are StaticTuple instances. If they
385
 *     aren't then cast self to a tuple, and have the tuple do the comparison.
386
 *  2) Special case comparison to Py_None, because it happens to occur fairly
387
 *     often in the test suite.
388
 *  3) Special case when v and w are the same pointer. As we know the answer to
389
 *     all queries without walking individual items.
390
 *  4) For all operations, we then walk the items to find the first paired
391
 *     items that are not equal.
392
 *  5) If all items found are equal, we then check the length of self and
393
 *     other to determine equality.
394
 *  6) If an item differs, then we apply "op" to those last two items. (eg.
395
 *     StaticTuple(A, B) > StaticTuple(A, C) iff B > C)
396
 */
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
397
398
static PyObject *
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
399
StaticTuple_richcompare(PyObject *v, PyObject *w, int op)
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
400
{
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
401
    StaticTuple *v_st, *w_st;
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
402
    Py_ssize_t vlen, wlen, min_len, i;
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
403
    PyObject *v_obj, *w_obj;
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
404
    richcmpfunc string_richcompare;
405
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
406
    if (!StaticTuple_CheckExact(v)) {
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
407
        /* This has never triggered, according to python-dev it seems this
408
         * might trigger if '__op__' is defined but '__rop__' is not, sort of
409
         * case. Such as "None == StaticTuple()"
410
         */
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
411
        fprintf(stderr, "self is not StaticTuple\n");
4679.3.23 by John Arbash Meinel
A bit more testing shows that we are comparing Key to tuples fairly often.
412
        Py_INCREF(Py_NotImplemented);
413
        return Py_NotImplemented;
414
    }
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
415
    v_st = (StaticTuple *)v;
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
416
    if (StaticTuple_CheckExact(w)) {
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
417
        /* The most common case */
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
418
        w_st = (StaticTuple*)w;
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
419
    } else if (PyTuple_Check(w)) {
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
420
        /* One of v or w is a tuple, so we go the 'slow' route and cast up to
421
         * tuples to compare.
422
         */
4679.3.23 by John Arbash Meinel
A bit more testing shows that we are comparing Key to tuples fairly often.
423
        /* TODO: This seems to be triggering more than I thought it would...
424
         *       We probably want to optimize comparing self to other when
425
         *       other is a tuple.
426
         */
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
427
        return StaticTuple_richcompare_to_tuple(v_st, w, op);
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
428
    } else if (w == Py_None) {
429
        // None is always less than the object
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
430
        switch (op) {
431
        case Py_NE:case Py_GT:case Py_GE:
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
432
            Py_INCREF(Py_True);
433
            return Py_True;
434
        case Py_EQ:case Py_LT:case Py_LE:
435
            Py_INCREF(Py_False);
436
            return Py_False;
4679.5.11 by John Arbash Meinel
add a hack-around to handle differences between pyrex 0.9.6 and 0.9.8
437
    default: // Should never happen
438
        return Py_NotImplemented;
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
439
        }
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
440
    } else {
441
        /* We don't special case this comparison, we just let python handle
442
         * it.
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
443
         */
444
         Py_INCREF(Py_NotImplemented);
445
         return Py_NotImplemented;
446
    }
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
447
    /* Now we know that we have 2 StaticTuple objects, so let's compare them.
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
448
     * This code is inspired from tuplerichcompare, except we know our
4679.3.45 by John Arbash Meinel
Do some work to handle comparison to object that aren't tuples or strings.
449
     * objects are limited in scope, so we can inline some comparisons.
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
450
     */
451
    if (v == w) {
452
        /* Identical pointers, we can shortcut this easily. */
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
453
        switch (op) {
454
        case Py_EQ:case Py_LE:case Py_GE:
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
455
            Py_INCREF(Py_True);
456
            return Py_True;
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
457
        case Py_NE:case Py_LT:case Py_GT:
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
458
            Py_INCREF(Py_False);
459
            return Py_False;
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
460
        }
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
461
    }
4679.5.6 by John Arbash Meinel
Some comment cleanup, implement a special case for Py_EQ when both objects are intrened.
462
    if (op == Py_EQ
463
        && _StaticTuple_is_interned(v_st)
464
        && _StaticTuple_is_interned(w_st))
465
    {
466
        /* If both objects are interned, we know they are different if the
467
         * pointer is not the same, which would have been handled by the
468
         * previous if. No need to compare the entries.
469
         */
470
        Py_INCREF(Py_False);
471
        return Py_False;
472
    }
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
473
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
474
    /* The only time we are likely to compare items of different lengths is in
475
     * something like the interned_keys set. However, the hash is good enough
476
     * that it is rare. Note that 'tuple_richcompare' also does not compare
477
     * lengths here.
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
478
     */
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
479
    vlen = v_st->size;
480
    wlen = w_st->size;
481
    min_len = (vlen < wlen) ? vlen : wlen;
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
482
    string_richcompare = PyBytes_Type.tp_richcompare;
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
483
    for (i = 0; i < min_len; i++) {
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
484
        PyObject *result = NULL;
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
485
        v_obj = StaticTuple_GET_ITEM(v_st, i);
486
        w_obj = StaticTuple_GET_ITEM(w_st, i);
4679.5.7 by John Arbash Meinel
Add a quick shortcut when comparing item-by-item.
487
        if (v_obj == w_obj) {
488
            /* Shortcut case, these must be identical */
489
            continue;
490
        }
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
491
        if (PyBytes_CheckExact(v_obj) && PyBytes_CheckExact(w_obj)) {
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
492
            result = string_richcompare(v_obj, w_obj, Py_EQ);
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
493
        } else if (StaticTuple_CheckExact(v_obj) &&
494
                   StaticTuple_CheckExact(w_obj))
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
495
        {
496
            /* Both are StaticTuple types, so recurse */
497
            result = StaticTuple_richcompare(v_obj, w_obj, Py_EQ);
498
        } else {
4759.2.9 by John Arbash Meinel
Implement support for lots of types.
499
            /* Fall back to generic richcompare */
500
            result = PyObject_RichCompare(v_obj, w_obj, Py_EQ);
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
501
        }
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
502
        if (result == NULL) {
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
503
            return NULL; /* There seems to be an error */
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
504
        }
505
        if (result == Py_False) {
4759.2.9 by John Arbash Meinel
Implement support for lots of types.
506
            // This entry is not identical, Shortcut for Py_EQ
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
507
            if (op == Py_EQ) {
508
                return result;
509
            }
510
            Py_DECREF(result);
511
            break;
512
        }
513
        if (result != Py_True) {
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
514
            /* We don't know *what* richcompare is returning, but it
515
             * isn't something we recognize
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
516
             */
517
            PyErr_BadInternalCall();
518
            Py_DECREF(result);
519
            return NULL;
520
        }
521
        Py_DECREF(result);
522
    }
4679.5.11 by John Arbash Meinel
add a hack-around to handle differences between pyrex 0.9.6 and 0.9.8
523
    if (i >= min_len) {
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
524
        /* We walked off one of the lists, but everything compared equal so
525
         * far. Just compare the size.
526
         */
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
527
        int cmp;
528
        PyObject *res;
529
        switch (op) {
530
        case Py_LT: cmp = vlen <  wlen; break;
531
        case Py_LE: cmp = vlen <= wlen; break;
532
        case Py_EQ: cmp = vlen == wlen; break;
533
        case Py_NE: cmp = vlen != wlen; break;
534
        case Py_GT: cmp = vlen >  wlen; break;
535
        case Py_GE: cmp = vlen >= wlen; break;
536
        default: return NULL; /* cannot happen */
537
        }
538
        if (cmp)
539
            res = Py_True;
540
        else
541
            res = Py_False;
542
        Py_INCREF(res);
543
        return res;
544
    }
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
545
    /* The last item differs, shortcut the Py_NE case */
546
    if (op == Py_NE) {
547
        Py_INCREF(Py_True);
548
        return Py_True;
549
    }
550
    /* It is some other comparison, go ahead and do the real check. */
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
551
    if (PyBytes_CheckExact(v_obj) && PyBytes_CheckExact(w_obj))
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
552
    {
553
        return string_richcompare(v_obj, w_obj, op);
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
554
    } else if (StaticTuple_CheckExact(v_obj) &&
555
               StaticTuple_CheckExact(w_obj))
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
556
    {
557
        /* Both are StaticTuple types, so recurse */
558
        return StaticTuple_richcompare(v_obj, w_obj, op);
559
    } else {
4759.2.9 by John Arbash Meinel
Implement support for lots of types.
560
        return PyObject_RichCompare(v_obj, w_obj, op);
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
561
    }
4679.3.21 by John Arbash Meinel
Implement Key_richcompare directly, rather than thunking to tuples.
562
}
563
564
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
565
static Py_ssize_t
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
566
StaticTuple_length(StaticTuple *self)
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
567
{
4679.3.37 by John Arbash Meinel
Change the Key header a bit. It is simpler if you use unsigned char
568
    return self->size;
4679.3.35 by John Arbash Meinel
Work on making intern() not generate immortal Key objects.
569
}
570
571
572
static PyObject *
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
573
StaticTuple__is_interned(StaticTuple *self)
4679.3.35 by John Arbash Meinel
Work on making intern() not generate immortal Key objects.
574
{
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
575
    if (_StaticTuple_is_interned(self)) {
4679.3.35 by John Arbash Meinel
Work on making intern() not generate immortal Key objects.
576
        Py_INCREF(Py_True);
577
        return Py_True;
578
    }
579
    Py_INCREF(Py_False);
580
    return Py_False;
581
}
582
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
583
static char StaticTuple__is_interned_doc[] = "_is_interned() => True/False\n"
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
584
    "Check to see if this tuple has been interned.\n";
4679.3.35 by John Arbash Meinel
Work on making intern() not generate immortal Key objects.
585
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
586
4759.2.1 by Andrew Bennetts
Implement number.add and coerce to allow concatenation with tuples.
587
static PyObject *
4759.2.18 by Matt Nordhoff
Working C implemention.
588
StaticTuple_reduce(StaticTuple *self)
589
{
590
    PyObject *result = NULL, *as_tuple = NULL;
591
592
    result = PyTuple_New(2);
593
    if (!result) {
594
        return NULL;
595
    }
596
    as_tuple = StaticTuple_as_tuple(self);
597
    if (as_tuple == NULL) {
4759.2.19 by Matt Nordhoff
Throw in a Py_DECREF.
598
        Py_DECREF(result);
4759.2.18 by Matt Nordhoff
Working C implemention.
599
        return NULL;
600
    }
4759.2.20 by Matt Nordhoff
Review: Add a Py_INCREF, and test pickling a nested StaticTuple.
601
    Py_INCREF(&StaticTuple_Type);
4759.2.18 by Matt Nordhoff
Working C implemention.
602
    PyTuple_SET_ITEM(result, 0, (PyObject *)&StaticTuple_Type);
603
    PyTuple_SET_ITEM(result, 1, as_tuple);
604
    return result;
605
}
606
607
static char StaticTuple_reduce_doc[] = "__reduce__() => tuple\n";
608
609
610
static PyObject *
4759.2.1 by Andrew Bennetts
Implement number.add and coerce to allow concatenation with tuples.
611
StaticTuple_add(PyObject *v, PyObject *w)
612
{
4759.2.7 by John Arbash Meinel
Re-implement StaticTuple_add() inline, rather that thunking over to tuples.
613
    Py_ssize_t i, len_v, len_w;
614
    PyObject *item;
615
    StaticTuple *result;
4759.2.5 by John Arbash Meinel
Add a bit more tests.
616
     /* StaticTuples and plain tuples may be added (concatenated) to
617
      * StaticTuples.
618
      */
619
    if (StaticTuple_CheckExact(v)) {
4759.2.7 by John Arbash Meinel
Re-implement StaticTuple_add() inline, rather that thunking over to tuples.
620
        len_v = ((StaticTuple*)v)->size;
621
    } else if (PyTuple_Check(v)) {
622
        len_v = PyTuple_GET_SIZE(v);
623
    } else {
624
        Py_INCREF(Py_NotImplemented);
625
        return Py_NotImplemented;
626
    }
4759.2.5 by John Arbash Meinel
Add a bit more tests.
627
    if (StaticTuple_CheckExact(w)) {
4759.2.7 by John Arbash Meinel
Re-implement StaticTuple_add() inline, rather that thunking over to tuples.
628
        len_w = ((StaticTuple*)w)->size;
629
    } else if (PyTuple_Check(w)) {
630
        len_w = PyTuple_GET_SIZE(w);
631
    } else {
632
        Py_INCREF(Py_NotImplemented);
633
        return Py_NotImplemented;
634
    }
635
    result = StaticTuple_New(len_v + len_w);
636
    if (result == NULL)
637
        return NULL;
638
    for (i = 0; i < len_v; ++i) {
639
        // This returns a new reference, which we then 'steal' with 
640
        // StaticTuple_SET_ITEM
641
        item = PySequence_GetItem(v, i);
642
        if (item == NULL) {
643
            Py_DECREF(result);
644
            return NULL;
645
        }
646
        StaticTuple_SET_ITEM(result, i, item);
647
    }
648
    for (i = 0; i < len_w; ++i) {
649
        item = PySequence_GetItem(w, i);
650
        if (item == NULL) {
651
            Py_DECREF(result);
652
            return NULL;
653
        }
654
        StaticTuple_SET_ITEM(result, i+len_v, item);
655
    }
656
    if (!StaticTuple_check_items(result)) {
657
        Py_DECREF(result);
658
        return NULL;
659
    }
660
    return (PyObject *)result;
4759.2.1 by Andrew Bennetts
Implement number.add and coerce to allow concatenation with tuples.
661
}
662
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
663
static PyObject *
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
664
StaticTuple_item(StaticTuple *self, Py_ssize_t offset)
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
665
{
666
    PyObject *obj;
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
667
    /* We cast to (int) to avoid worrying about whether Py_ssize_t is a
668
     * long long, etc. offsets should never be >2**31 anyway.
669
     */
670
    if (offset < 0) {
671
        PyErr_Format(PyExc_IndexError, "StaticTuple_item does not support"
672
            " negative indices: %d\n", (int)offset);
673
    } else if (offset >= self->size) {
674
        PyErr_Format(PyExc_IndexError, "StaticTuple index out of range"
675
            " %d >= %d", (int)offset, (int)self->size);
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
676
        return NULL;
677
    }
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
678
    obj = (PyObject *)self->items[offset];
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
679
    Py_INCREF(obj);
680
    return obj;
681
}
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
682
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
683
#if PY_MAJOR_VERSION >= 3
684
#else
4679.3.19 by John Arbash Meinel
implement slicing as a tuple thunk.
685
static PyObject *
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
686
StaticTuple_slice(StaticTuple *self, Py_ssize_t ilow, Py_ssize_t ihigh)
4679.3.19 by John Arbash Meinel
implement slicing as a tuple thunk.
687
{
688
    PyObject *as_tuple, *result;
689
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
690
    as_tuple = StaticTuple_as_tuple(self);
4679.3.19 by John Arbash Meinel
implement slicing as a tuple thunk.
691
    if (as_tuple == NULL) {
692
        return NULL;
693
    }
694
    result = PyTuple_Type.tp_as_sequence->sq_slice(as_tuple, ilow, ihigh);
695
    Py_DECREF(as_tuple);
696
    return result;
697
}
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
698
#endif
699
700
static PyObject *
701
StaticTuple_subscript(StaticTuple *self, PyObject *key)
702
{
703
    PyObject *as_tuple, *result;
704
705
    as_tuple = StaticTuple_as_tuple(self);
706
    if (as_tuple == NULL) {
707
        return NULL;
708
    }
709
    result = PyTuple_Type.tp_as_mapping->mp_subscript(as_tuple, key);
710
    Py_DECREF(as_tuple);
711
    return result;
712
}
4679.3.19 by John Arbash Meinel
implement slicing as a tuple thunk.
713
4679.3.20 by John Arbash Meinel
Implement tp_traverse, and add a soft dependency on meliae to test it.
714
static int
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
715
StaticTuple_traverse(StaticTuple *self, visitproc visit, void *arg)
4679.3.20 by John Arbash Meinel
Implement tp_traverse, and add a soft dependency on meliae to test it.
716
{
717
    Py_ssize_t i;
4679.3.46 by John Arbash Meinel
Play around a bit with changing the hash function.
718
    for (i = self->size; --i >= 0;) {
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
719
        Py_VISIT(self->items[i]);
4679.3.20 by John Arbash Meinel
Implement tp_traverse, and add a soft dependency on meliae to test it.
720
    }
721
    return 0;
722
}
723
5320.1.1 by Andrew Bennetts
Implement __sizeof__ in StaticTuple.
724
725
static PyObject *
726
StaticTuple_sizeof(StaticTuple *self)
727
{
728
	Py_ssize_t res;
729
730
	res = _PyObject_SIZE(&StaticTuple_Type) + (int)self->size * sizeof(void*);
731
	return PyInt_FromSsize_t(res);
732
}
733
734
735
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
736
static char StaticTuple_doc[] =
737
    "C implementation of a StaticTuple structure."
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
738
    "\n This is used as StaticTuple(item1, item2, item3)"
739
    "\n This is similar to tuple, less flexible in what it"
740
    "\n supports, but also lighter memory consumption."
741
    "\n Note that the constructor mimics the () form of tuples"
742
    "\n Rather than the 'tuple()' constructor."
743
    "\n  eg. StaticTuple(a, b) == (a, b) == tuple((a, b))";
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
744
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
745
static PyMethodDef StaticTuple_methods[] = {
746
    {"as_tuple", (PyCFunction)StaticTuple_as_tuple, METH_NOARGS, StaticTuple_as_tuple_doc},
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
747
    {"intern", (PyCFunction)StaticTuple_Intern, METH_NOARGS, StaticTuple_Intern_doc},
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
748
    {"_is_interned", (PyCFunction)StaticTuple__is_interned, METH_NOARGS,
749
     StaticTuple__is_interned_doc},
4739.4.1 by John Arbash Meinel
Implement StaticTuple.from_sequence()
750
    {"from_sequence", (PyCFunction)StaticTuple_from_sequence,
751
     METH_STATIC | METH_VARARGS,
752
     "Create a StaticTuple from a given sequence. This functions"
753
     " the same as the tuple() constructor."},
4759.2.18 by Matt Nordhoff
Working C implemention.
754
    {"__reduce__", (PyCFunction)StaticTuple_reduce, METH_NOARGS, StaticTuple_reduce_doc},
5320.1.1 by Andrew Bennetts
Implement __sizeof__ in StaticTuple.
755
    {"__sizeof__",  (PyCFunction)StaticTuple_sizeof, METH_NOARGS}, 
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
756
    {NULL, NULL} /* sentinel */
757
};
758
4759.2.1 by Andrew Bennetts
Implement number.add and coerce to allow concatenation with tuples.
759
760
static PyNumberMethods StaticTuple_as_number = {
4759.2.5 by John Arbash Meinel
Add a bit more tests.
761
    (binaryfunc) StaticTuple_add,   /* nb_add */
762
    0,                              /* nb_subtract */
763
    0,                              /* nb_multiply */
764
    0,                              /* nb_divide */
765
    0,                              /* nb_remainder */
766
    0,                              /* nb_divmod */
767
    0,                              /* nb_power */
768
    0,                              /* nb_negative */
769
    0,                              /* nb_positive */
770
    0,                              /* nb_absolute */
771
    0,                              /* nb_nonzero */
772
    0,                              /* nb_invert */
773
    0,                              /* nb_lshift */
774
    0,                              /* nb_rshift */
775
    0,                              /* nb_and */
776
    0,                              /* nb_xor */
777
    0,                              /* nb_or */
778
    0,                              /* nb_coerce */
4759.2.1 by Andrew Bennetts
Implement number.add and coerce to allow concatenation with tuples.
779
};
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
780
4759.2.1 by Andrew Bennetts
Implement number.add and coerce to allow concatenation with tuples.
781
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
782
static PySequenceMethods StaticTuple_as_sequence = {
783
    (lenfunc)StaticTuple_length,            /* sq_length */
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
784
    0,                              /* sq_concat */
785
    0,                              /* sq_repeat */
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
786
    (ssizeargfunc)StaticTuple_item,         /* sq_item */
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
787
#if PY_MAJOR_VERSION >= 3
788
#else
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
789
    (ssizessizeargfunc)StaticTuple_slice,   /* sq_slice */
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
790
#endif
4679.3.17 by John Arbash Meinel
Implement some of the sequence items.
791
    0,                              /* sq_ass_item */
792
    0,                              /* sq_ass_slice */
793
    0,                              /* sq_contains */
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
794
#if PY_MAJOR_VERSION >= 3
795
    0,                              /* sq_inplace_concat */
796
    0,                              /* sq_inplace_repeat */
797
#endif
798
};
799
800
801
static PyMappingMethods StaticTuple_as_mapping = {
802
    (lenfunc)StaticTuple_length,            /* mp_length */
803
    (binaryfunc)StaticTuple_subscript,      /* mp_subscript */
804
    0,                                      /* mp_ass_subscript */
805
};
4679.5.5 by John Arbash Meinel
Review feedback from Andrew Bennetts.
806
4679.4.1 by John Arbash Meinel
Handle some issues with static/etc to get things to build on babune.
807
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
808
PyTypeObject StaticTuple_Type = {
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
809
    PyVarObject_HEAD_INIT(NULL, 0)
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
810
    "breezy._static_tuple_c.StaticTuple",        /* tp_name */
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
811
    sizeof(StaticTuple),                         /* tp_basicsize */
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
812
    sizeof(PyObject *),                          /* tp_itemsize */
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
813
    (destructor)StaticTuple_dealloc,             /* tp_dealloc */
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
814
    0,                                           /* tp_print */
815
    0,                                           /* tp_getattr */
816
    0,                                           /* tp_setattr */
817
    0,                                           /* tp_compare */
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
818
    (reprfunc)StaticTuple_repr,                  /* tp_repr */
4759.2.1 by Andrew Bennetts
Implement number.add and coerce to allow concatenation with tuples.
819
    &StaticTuple_as_number,                      /* tp_as_number */
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
820
    &StaticTuple_as_sequence,                    /* tp_as_sequence */
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
821
    &StaticTuple_as_mapping,                     /* tp_as_mapping */
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
822
    (hashfunc)StaticTuple_hash,                  /* tp_hash */
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
823
    0,                                           /* tp_call */
824
    0,                                           /* tp_str */
4747.1.1 by John Arbash Meinel
On Windows w/ Python2.5 PyObject_GenericGetAttr is an external DLL function,
825
    0,                                           /* tp_getattro */
4679.3.16 by John Arbash Meinel
Initial work for a Key class.
826
    0,                                           /* tp_setattro */
827
    0,                                           /* tp_as_buffer */
4759.2.5 by John Arbash Meinel
Add a bit more tests.
828
    /* Py_TPFLAGS_CHECKTYPES tells the number operations that they shouldn't
829
     * try to 'coerce' but instead stuff like 'add' will check it arguments.
830
     */
831
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES,  /* tp_flags*/
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
832
    StaticTuple_doc,                             /* tp_doc */
4679.3.20 by John Arbash Meinel
Implement tp_traverse, and add a soft dependency on meliae to test it.
833
    /* gc.get_referents checks the IS_GC flag before it calls tp_traverse
834
     * And we don't include this object in the garbage collector because we
835
     * know it doesn't create cycles. However, 'meliae' will follow
836
     * tp_traverse, even if the object isn't GC, and we want that.
837
     */
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
838
    (traverseproc)StaticTuple_traverse,          /* tp_traverse */
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
839
    0,                                           /* tp_clear */
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
840
    StaticTuple_richcompare,                     /* tp_richcompare */
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
841
    0,                                           /* tp_weaklistoffset */
4679.3.76 by John Arbash Meinel
Rename StaticTupleInterner => SimpleSet.
842
    // without implementing tp_iter, Python will fall back to PySequence*
843
    // which seems to work ok, we may need something faster/lighter in the
844
    // future.
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
845
    0,                                           /* tp_iter */
846
    0,                                           /* tp_iternext */
4679.3.42 by John Arbash Meinel
Implement comparison support when using nested StaticTuple objects.
847
    StaticTuple_methods,                         /* tp_methods */
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
848
    0,                                           /* tp_members */
849
    0,                                           /* tp_getset */
850
    0,                                           /* tp_base */
851
    0,                                           /* tp_dict */
852
    0,                                           /* tp_descr_get */
853
    0,                                           /* tp_descr_set */
854
    0,                                           /* tp_dictoffset */
855
    0,                                           /* tp_init */
856
    0,                                           /* tp_alloc */
4679.5.9 by John Arbash Meinel
NEWS entry for StaticTuple class.
857
    StaticTuple_new_constructor,                 /* tp_new */
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
858
};
859
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
860
4679.3.41 by John Arbash Meinel
Finish switching the naming to StaticTuple.
861
static PyMethodDef static_tuple_c_methods[] = {
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
862
    {NULL, NULL}
863
};
864
865
4679.3.47 by John Arbash Meinel
Work out how to expose the C api using the Python PyCObject interface.
866
static void
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
867
setup_interned_tuples(PyObject *m)
4679.3.1 by John Arbash Meinel
Start working on a Keys type.
868
{
4679.3.76 by John Arbash Meinel
Rename StaticTupleInterner => SimpleSet.
869
    _interned_tuples = (PyObject *)SimpleSet_New();
4679.3.51 by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code.
870
    if (_interned_tuples != NULL) {
871
        Py_INCREF(_interned_tuples);
872
        PyModule_AddObject(m, "_interned_tuples", _interned_tuples);
4679.3.29 by John Arbash Meinel
Start work on implementing a Key.intern() function.
873
    }
4679.3.47 by John Arbash Meinel
Work out how to expose the C api using the Python PyCObject interface.
874
}
875
876
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
877
static void
878
setup_empty_tuple(PyObject *m)
879
{
880
    StaticTuple *stuple;
881
    if (_interned_tuples == NULL) {
882
        fprintf(stderr, "You need to call setup_interned_tuples() before"
883
                " setup_empty_tuple, because we intern it.\n");
884
    }
885
    // We need to create the empty tuple
886
    stuple = (StaticTuple *)StaticTuple_New(0);
887
    _empty_tuple = StaticTuple_Intern(stuple);
888
    assert(_empty_tuple == stuple);
889
    // At this point, refcnt is 2: 1 from New(), and 1 from the return from
890
    // intern(). We will keep 1 for the _empty_tuple global, and use the other
891
    // for the module reference.
892
    PyModule_AddObject(m, "_empty_tuple", (PyObject *)_empty_tuple);
893
}
894
895
static int
896
_StaticTuple_CheckExact(PyObject *obj)
897
{
898
    return StaticTuple_CheckExact(obj);
899
}
900
901
static void
902
setup_c_api(PyObject *m)
903
{
904
    _export_function(m, "StaticTuple_New", StaticTuple_New,
905
        "StaticTuple *(Py_ssize_t)");
906
    _export_function(m, "StaticTuple_Intern", StaticTuple_Intern,
907
        "StaticTuple *(StaticTuple *)");
4739.4.1 by John Arbash Meinel
Implement StaticTuple.from_sequence()
908
    _export_function(m, "StaticTuple_FromSequence", StaticTuple_FromSequence,
909
        "StaticTuple *(PyObject *)");
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
910
    _export_function(m, "_StaticTuple_CheckExact", _StaticTuple_CheckExact,
911
        "int(PyObject *)");
912
}
913
914
6715.1.4 by Martin
Correct name for init func
915
PYMOD_INIT_FUNC(_static_tuple_c)
4679.3.47 by John Arbash Meinel
Work out how to expose the C api using the Python PyCObject interface.
916
{
917
    PyObject* m;
918
4747.1.1 by John Arbash Meinel
On Windows w/ Python2.5 PyObject_GenericGetAttr is an external DLL function,
919
    StaticTuple_Type.tp_getattro = PyObject_GenericGetAttr;
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
920
    if (PyType_Ready(&StaticTuple_Type) < 0) {
921
        return PYMOD_ERROR;
922
    }
4679.3.47 by John Arbash Meinel
Work out how to expose the C api using the Python PyCObject interface.
923
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
924
    PYMOD_CREATE(m, "_static_tuple_c",
925
                 "C implementation of a StaticTuple structure",
926
                 static_tuple_c_methods);
927
    if (m == NULL) {
928
      return PYMOD_ERROR;
929
    }
4679.3.47 by John Arbash Meinel
Work out how to expose the C api using the Python PyCObject interface.
930
931
    Py_INCREF(&StaticTuple_Type);
932
    PyModule_AddObject(m, "StaticTuple", (PyObject *)&StaticTuple_Type);
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
933
    if (import_breezy___simple_set_pyx() == -1) {
934
        return PYMOD_ERROR;
4679.3.81 by John Arbash Meinel
Fix up _simple_set_pyx.pyx to be compatible with pyrex again.
935
    }
4679.3.74 by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code.
936
    setup_interned_tuples(m);
937
    setup_empty_tuple(m);
938
    setup_c_api(m);
6715.1.1 by Martin
Make _static_tuple_c compile on Python 3
939
940
    return PYMOD_SUCCESS(m);
4679.3.47 by John Arbash Meinel
Work out how to expose the C api using the Python PyCObject interface.
941
}
4759.2.5 by John Arbash Meinel
Add a bit more tests.
942
943
// vim: tabstop=4 sw=4 expandtab