1
/* Copyright (C) 2009 Canonical Ltd
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.
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.
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
18
/* Must be defined before importing _static_tuple_c.h so that we get the right
21
#define STATIC_TUPLE_MODULE
24
#include "python-compat.h"
26
#include "_static_tuple_c.h"
27
#include "_export_c_api.h"
29
/* Pyrex 0.9.6.4 exports _simple_set_pyx_api as
30
* import__simple_set_pyx(), while Pyrex 0.9.8.5 and Cython 0.11.3 export them
31
* as import_bzrlib___simple_set_pyx(). As such, we just #define one to be
32
* equivalent to the other in our internal code.
34
#define import__simple_set_pyx import_bzrlib___simple_set_pyx
35
#include "_simple_set_pyx_api.h"
38
# define inline __inline__
39
#elif defined(_MSC_VER)
40
# define inline __inline
46
/* The one and only StaticTuple with no values */
47
static StaticTuple *_empty_tuple = NULL;
48
static PyObject *_interned_tuples = NULL;
52
_StaticTuple_is_interned(StaticTuple *self)
54
return self->flags & STATIC_TUPLE_INTERNED_FLAG;
60
StaticTuple_as_tuple(StaticTuple *self)
62
PyObject *tpl = NULL, *obj = NULL;
66
tpl = PyTuple_New(len);
71
for (i = 0; i < len; ++i) {
72
obj = (PyObject *)self->items[i];
74
PyTuple_SET_ITEM(tpl, i, obj);
80
static char StaticTuple_as_tuple_doc[] = "as_tuple() => tuple";
83
StaticTuple_Intern(StaticTuple *self)
85
PyObject *canonical_tuple = NULL;
87
if (_interned_tuples == NULL || _StaticTuple_is_interned(self)) {
91
/* SimpleSet_Add returns whatever object is present at self
92
* or the new object if it needs to add it.
94
canonical_tuple = SimpleSet_Add(_interned_tuples, (PyObject *)self);
95
if (!canonical_tuple) {
96
// Some sort of exception, propogate it.
99
if (canonical_tuple != (PyObject *)self) {
100
// There was already a tuple with that value
101
return (StaticTuple *)canonical_tuple;
103
self->flags |= STATIC_TUPLE_INTERNED_FLAG;
104
// The two references in the dict do not count, so that the StaticTuple
105
// object does not become immortal just because it was interned.
106
Py_REFCNT(self) -= 1;
110
static char StaticTuple_Intern_doc[] = "intern() => unique StaticTuple\n"
111
"Return a 'canonical' StaticTuple object.\n"
112
"Similar to intern() for strings, this makes sure there\n"
113
"is only one StaticTuple object for a given value\n."
115
" key = StaticTuple('foo', 'bar').intern()\n";
119
StaticTuple_dealloc(StaticTuple *self)
123
if (_StaticTuple_is_interned(self)) {
124
/* revive dead object temporarily for Discard */
126
if (SimpleSet_Discard(_interned_tuples, (PyObject*)self) != 1)
127
Py_FatalError("deletion of interned StaticTuple failed");
128
self->flags &= ~STATIC_TUPLE_INTERNED_FLAG;
131
for (i = 0; i < len; ++i) {
132
Py_XDECREF(self->items[i]);
134
Py_TYPE(self)->tp_free((PyObject *)self);
138
/* Similar to PyTuple_New() */
140
StaticTuple_New(Py_ssize_t size)
144
PyErr_BadInternalCall();
148
if (size == 0 && _empty_tuple != NULL) {
149
Py_INCREF(_empty_tuple);
152
/* Note that we use PyObject_NewVar because we want to allocate a variable
153
* width entry. However we *aren't* truly a PyVarObject because we don't
154
* use a long for ob_size. Instead we use a plain 'size' that is an int,
155
* and will be overloaded with flags in the future.
156
* As such we do the alloc, and then have to clean up anything it does
159
stuple = PyObject_NewVar(StaticTuple, &StaticTuple_Type, size);
160
if (stuple == NULL) {
165
stuple->_unused0 = 0;
166
stuple->_unused1 = 0;
168
memset(stuple->items, 0, sizeof(PyObject *) * size);
170
#if STATIC_TUPLE_HAS_HASH
178
StaticTuple_new_constructor(PyTypeObject *type, PyObject *args, PyObject *kwds)
181
PyObject *obj = NULL;
182
Py_ssize_t i, len = 0;
184
if (type != &StaticTuple_Type) {
185
PyErr_SetString(PyExc_TypeError, "we only support creating StaticTuple");
188
if (!PyTuple_CheckExact(args)) {
189
PyErr_SetString(PyExc_TypeError, "args must be a tuple");
192
len = PyTuple_GET_SIZE(args);
193
if (len < 0 || len > 255) {
194
/* Too big or too small */
195
PyErr_SetString(PyExc_ValueError, "StaticTuple.__init__(...)"
196
" takes from 0 to 255 items");
199
self = (StaticTuple *)StaticTuple_New(len);
203
for (i = 0; i < len; ++i) {
204
obj = PyTuple_GET_ITEM(args, i);
205
if (!PyString_CheckExact(obj)) {
206
if (!StaticTuple_CheckExact(obj)) {
207
PyErr_SetString(PyExc_TypeError, "StaticTuple.__init__(...)"
208
" requires that all items are strings or StaticTuple.");
209
type->tp_dealloc((PyObject *)self);
214
self->items[i] = obj;
216
return (PyObject *)self;
220
StaticTuple_repr(StaticTuple *self)
222
PyObject *as_tuple, *tuple_repr, *result;
224
as_tuple = StaticTuple_as_tuple(self);
225
if (as_tuple == NULL) {
228
tuple_repr = PyObject_Repr(as_tuple);
230
if (tuple_repr == NULL) {
233
result = PyString_FromFormat("%s%s", Py_TYPE(self)->tp_name,
234
PyString_AsString(tuple_repr));
239
StaticTuple_hash(StaticTuple *self)
241
/* adapted from tuplehash(), is the specific hash value considered
245
Py_ssize_t len = self->size;
247
long mult = 1000003L;
249
#if STATIC_TUPLE_HAS_HASH
250
if (self->hash != -1) {
256
// TODO: We could set specific flags if we know that, for example, all the
257
// items are strings. I haven't seen a real-world benefit to that
260
y = PyObject_Hash(*p++);
261
if (y == -1) /* failure */
264
/* the cast might truncate len; that doesn't change hash stability */
265
mult += (long)(82520L + len + len);
270
#if STATIC_TUPLE_HAS_HASH
277
StaticTuple_richcompare_to_tuple(StaticTuple *v, PyObject *wt, int op)
280
PyObject *result = NULL;
282
vt = StaticTuple_as_tuple((StaticTuple *)v);
286
if (!PyTuple_Check(wt)) {
287
PyErr_BadInternalCall();
290
/* Now we have 2 tuples to compare, do it */
291
result = PyTuple_Type.tp_richcompare(vt, wt, op);
297
/** Compare two objects to determine if they are equivalent.
298
* The basic flow is as follows
299
* 1) First make sure that both objects are StaticTuple instances. If they
300
* aren't then cast self to a tuple, and have the tuple do the comparison.
301
* 2) Special case comparison to Py_None, because it happens to occur fairly
302
* often in the test suite.
303
* 3) Special case when v and w are the same pointer. As we know the answer to
304
* all queries without walking individual items.
305
* 4) For all operations, we then walk the items to find the first paired
306
* items that are not equal.
307
* 5) If all items found are equal, we then check the length of self and
308
* other to determine equality.
309
* 6) If an item differs, then we apply "op" to those last two items. (eg.
310
* StaticTuple(A, B) > StaticTuple(A, C) iff B > C)
314
StaticTuple_richcompare(PyObject *v, PyObject *w, int op)
316
StaticTuple *v_st, *w_st;
317
Py_ssize_t vlen, wlen, min_len, i;
318
PyObject *v_obj, *w_obj;
319
richcmpfunc string_richcompare;
321
if (!StaticTuple_CheckExact(v)) {
322
/* This has never triggered, according to python-dev it seems this
323
* might trigger if '__op__' is defined but '__rop__' is not, sort of
324
* case. Such as "None == StaticTuple()"
326
fprintf(stderr, "self is not StaticTuple\n");
327
Py_INCREF(Py_NotImplemented);
328
return Py_NotImplemented;
330
v_st = (StaticTuple *)v;
331
if (StaticTuple_CheckExact(w)) {
332
/* The most common case */
333
w_st = (StaticTuple*)w;
334
} else if (PyTuple_Check(w)) {
335
/* One of v or w is a tuple, so we go the 'slow' route and cast up to
338
/* TODO: This seems to be triggering more than I thought it would...
339
* We probably want to optimize comparing self to other when
342
return StaticTuple_richcompare_to_tuple(v_st, w, op);
343
} else if (w == Py_None) {
344
// None is always less than the object
346
case Py_NE:case Py_GT:case Py_GE:
349
case Py_EQ:case Py_LT:case Py_LE:
352
default: // Should never happen
353
return Py_NotImplemented;
356
/* We don't special case this comparison, we just let python handle
359
Py_INCREF(Py_NotImplemented);
360
return Py_NotImplemented;
362
/* Now we know that we have 2 StaticTuple objects, so let's compare them.
363
* This code is inspired from tuplerichcompare, except we know our
364
* objects are limited in scope, so we can inline some comparisons.
367
/* Identical pointers, we can shortcut this easily. */
369
case Py_EQ:case Py_LE:case Py_GE:
372
case Py_NE:case Py_LT:case Py_GT:
378
&& _StaticTuple_is_interned(v_st)
379
&& _StaticTuple_is_interned(w_st))
381
/* If both objects are interned, we know they are different if the
382
* pointer is not the same, which would have been handled by the
383
* previous if. No need to compare the entries.
389
/* The only time we are likely to compare items of different lengths is in
390
* something like the interned_keys set. However, the hash is good enough
391
* that it is rare. Note that 'tuple_richcompare' also does not compare
396
min_len = (vlen < wlen) ? vlen : wlen;
397
string_richcompare = PyString_Type.tp_richcompare;
398
for (i = 0; i < min_len; i++) {
399
PyObject *result = NULL;
400
v_obj = StaticTuple_GET_ITEM(v_st, i);
401
w_obj = StaticTuple_GET_ITEM(w_st, i);
402
if (v_obj == w_obj) {
403
/* Shortcut case, these must be identical */
406
if (PyString_CheckExact(v_obj) && PyString_CheckExact(w_obj)) {
407
result = string_richcompare(v_obj, w_obj, Py_EQ);
408
} else if (StaticTuple_CheckExact(v_obj) &&
409
StaticTuple_CheckExact(w_obj))
411
/* Both are StaticTuple types, so recurse */
412
result = StaticTuple_richcompare(v_obj, w_obj, Py_EQ);
414
/* Not the same type, obviously they won't compare equal */
417
if (result == NULL) {
418
return NULL; /* There seems to be an error */
420
if (result == Py_NotImplemented) {
421
PyErr_BadInternalCall();
425
if (result == Py_False) {
426
/* This entry is not identical
435
if (result != Py_True) {
436
/* We don't know *what* richcompare is returning, but it
437
* isn't something we recognize
439
PyErr_BadInternalCall();
446
/* We walked off one of the lists, but everything compared equal so
447
* far. Just compare the size.
452
case Py_LT: cmp = vlen < wlen; break;
453
case Py_LE: cmp = vlen <= wlen; break;
454
case Py_EQ: cmp = vlen == wlen; break;
455
case Py_NE: cmp = vlen != wlen; break;
456
case Py_GT: cmp = vlen > wlen; break;
457
case Py_GE: cmp = vlen >= wlen; break;
458
default: return NULL; /* cannot happen */
467
/* The last item differs, shortcut the Py_NE case */
472
/* It is some other comparison, go ahead and do the real check. */
473
if (PyString_CheckExact(v_obj) && PyString_CheckExact(w_obj))
475
return string_richcompare(v_obj, w_obj, op);
476
} else if (StaticTuple_CheckExact(v_obj) &&
477
StaticTuple_CheckExact(w_obj))
479
/* Both are StaticTuple types, so recurse */
480
return StaticTuple_richcompare(v_obj, w_obj, op);
482
Py_INCREF(Py_NotImplemented);
483
return Py_NotImplemented;
489
StaticTuple_length(StaticTuple *self)
496
StaticTuple__is_interned(StaticTuple *self)
498
if (_StaticTuple_is_interned(self)) {
506
static char StaticTuple__is_interned_doc[] = "_is_interned() => True/False\n"
507
"Check to see if this tuple has been interned.\n";
511
StaticTuple_item(StaticTuple *self, Py_ssize_t offset)
514
/* We cast to (int) to avoid worrying about whether Py_ssize_t is a
515
* long long, etc. offsets should never be >2**31 anyway.
518
PyErr_Format(PyExc_IndexError, "StaticTuple_item does not support"
519
" negative indices: %d\n", (int)offset);
520
} else if (offset >= self->size) {
521
PyErr_Format(PyExc_IndexError, "StaticTuple index out of range"
522
" %d >= %d", (int)offset, (int)self->size);
525
obj = (PyObject *)self->items[offset];
531
StaticTuple_slice(StaticTuple *self, Py_ssize_t ilow, Py_ssize_t ihigh)
533
PyObject *as_tuple, *result;
535
as_tuple = StaticTuple_as_tuple(self);
536
if (as_tuple == NULL) {
539
result = PyTuple_Type.tp_as_sequence->sq_slice(as_tuple, ilow, ihigh);
545
StaticTuple_traverse(StaticTuple *self, visitproc visit, void *arg)
548
for (i = self->size; --i >= 0;) {
549
Py_VISIT(self->items[i]);
554
static char StaticTuple_doc[] =
555
"C implementation of a StaticTuple structure."
556
"\n This is used as StaticTuple(item1, item2, item3)"
557
"\n This is similar to tuple, less flexible in what it"
558
"\n supports, but also lighter memory consumption."
559
"\n Note that the constructor mimics the () form of tuples"
560
"\n Rather than the 'tuple()' constructor."
561
"\n eg. StaticTuple(a, b) == (a, b) == tuple((a, b))";
563
static PyMethodDef StaticTuple_methods[] = {
564
{"as_tuple", (PyCFunction)StaticTuple_as_tuple, METH_NOARGS, StaticTuple_as_tuple_doc},
565
{"intern", (PyCFunction)StaticTuple_Intern, METH_NOARGS, StaticTuple_Intern_doc},
566
{"_is_interned", (PyCFunction)StaticTuple__is_interned, METH_NOARGS,
567
StaticTuple__is_interned_doc},
568
{NULL, NULL} /* sentinel */
571
static PySequenceMethods StaticTuple_as_sequence = {
572
(lenfunc)StaticTuple_length, /* sq_length */
575
(ssizeargfunc)StaticTuple_item, /* sq_item */
576
(ssizessizeargfunc)StaticTuple_slice, /* sq_slice */
578
0, /* sq_ass_slice */
582
/* TODO: Implement StaticTuple_as_mapping.
583
* The only thing we really want to support from there is mp_subscript,
584
* so that we could support extended slicing (foo[::2]). Not worth it
589
PyTypeObject StaticTuple_Type = {
590
PyObject_HEAD_INIT(NULL)
592
"StaticTuple", /* tp_name */
593
sizeof(StaticTuple), /* tp_basicsize */
594
sizeof(PyObject *), /* tp_itemsize */
595
(destructor)StaticTuple_dealloc, /* tp_dealloc */
600
(reprfunc)StaticTuple_repr, /* tp_repr */
601
0, /* tp_as_number */
602
&StaticTuple_as_sequence, /* tp_as_sequence */
603
0, /* tp_as_mapping */
604
(hashfunc)StaticTuple_hash, /* tp_hash */
607
PyObject_GenericGetAttr, /* tp_getattro */
609
0, /* tp_as_buffer */
610
Py_TPFLAGS_DEFAULT, /* tp_flags*/
611
StaticTuple_doc, /* tp_doc */
612
/* gc.get_referents checks the IS_GC flag before it calls tp_traverse
613
* And we don't include this object in the garbage collector because we
614
* know it doesn't create cycles. However, 'meliae' will follow
615
* tp_traverse, even if the object isn't GC, and we want that.
617
(traverseproc)StaticTuple_traverse, /* tp_traverse */
619
StaticTuple_richcompare, /* tp_richcompare */
620
0, /* tp_weaklistoffset */
621
// without implementing tp_iter, Python will fall back to PySequence*
622
// which seems to work ok, we may need something faster/lighter in the
626
StaticTuple_methods, /* tp_methods */
631
0, /* tp_descr_get */
632
0, /* tp_descr_set */
633
0, /* tp_dictoffset */
636
StaticTuple_new_constructor, /* tp_new */
640
static PyMethodDef static_tuple_c_methods[] = {
646
setup_interned_tuples(PyObject *m)
648
_interned_tuples = (PyObject *)SimpleSet_New();
649
if (_interned_tuples != NULL) {
650
Py_INCREF(_interned_tuples);
651
PyModule_AddObject(m, "_interned_tuples", _interned_tuples);
657
setup_empty_tuple(PyObject *m)
660
if (_interned_tuples == NULL) {
661
fprintf(stderr, "You need to call setup_interned_tuples() before"
662
" setup_empty_tuple, because we intern it.\n");
664
// We need to create the empty tuple
665
stuple = (StaticTuple *)StaticTuple_New(0);
666
_empty_tuple = StaticTuple_Intern(stuple);
667
assert(_empty_tuple == stuple);
668
// At this point, refcnt is 2: 1 from New(), and 1 from the return from
669
// intern(). We will keep 1 for the _empty_tuple global, and use the other
670
// for the module reference.
671
PyModule_AddObject(m, "_empty_tuple", (PyObject *)_empty_tuple);
675
_StaticTuple_CheckExact(PyObject *obj)
677
return StaticTuple_CheckExact(obj);
681
setup_c_api(PyObject *m)
683
_export_function(m, "StaticTuple_New", StaticTuple_New,
684
"StaticTuple *(Py_ssize_t)");
685
_export_function(m, "StaticTuple_Intern", StaticTuple_Intern,
686
"StaticTuple *(StaticTuple *)");
687
_export_function(m, "_StaticTuple_CheckExact", _StaticTuple_CheckExact,
693
_workaround_pyrex_096(void)
695
/* Work around an incompatibility in how pyrex 0.9.6 exports a module,
696
* versus how pyrex 0.9.8 and cython 0.11 export it.
697
* Namely 0.9.6 exports import__simple_set_pyx and tries to
698
* "import _simple_set_pyx" but it is available only as
699
* "import bzrlib._simple_set_pyx"
700
* It is a shame to hack up sys.modules, but that is what we've got to do.
702
PyObject *sys_module = NULL, *modules = NULL, *set_module = NULL;
705
/* Clear out the current ImportError exception, and try again. */
707
/* Note that this only seems to work if somewhere else imports
708
* bzrlib._simple_set_pyx before importing bzrlib._static_tuple_c
710
set_module = PyImport_ImportModule("bzrlib._simple_set_pyx");
711
if (set_module == NULL) {
712
// fprintf(stderr, "Failed to import bzrlib._simple_set_pyx\n");
715
/* Add the _simple_set_pyx into sys.modules at the appropriate location. */
716
sys_module = PyImport_ImportModule("sys");
717
if (sys_module == NULL) {
718
// fprintf(stderr, "Failed to import sys\n");
721
modules = PyObject_GetAttrString(sys_module, "modules");
722
if (modules == NULL || !PyDict_Check(modules)) {
723
// fprintf(stderr, "Failed to find sys.modules\n");
726
PyDict_SetItemString(modules, "_simple_set_pyx", set_module);
727
/* Now that we have hacked it in, try the import again. */
728
retval = import_bzrlib___simple_set_pyx();
730
Py_XDECREF(set_module);
731
Py_XDECREF(sys_module);
738
init_static_tuple_c(void)
742
if (PyType_Ready(&StaticTuple_Type) < 0)
745
m = Py_InitModule3("_static_tuple_c", static_tuple_c_methods,
746
"C implementation of a StaticTuple structure");
750
Py_INCREF(&StaticTuple_Type);
751
PyModule_AddObject(m, "StaticTuple", (PyObject *)&StaticTuple_Type);
752
if (import_bzrlib___simple_set_pyx() == -1
753
&& _workaround_pyrex_096() == -1)
757
setup_interned_tuples(m);
758
setup_empty_tuple(m);