26
26
#include "_static_tuple_c.h"
27
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
29
35
#include "_simple_set_pyx_api.h"
31
37
#if defined(__GNUC__)
236
242
" should not have a NULL entry.");
239
if (PyBytes_CheckExact(obj)
245
if (PyString_CheckExact(obj)
240
246
|| StaticTuple_CheckExact(obj)
241
247
|| obj == Py_None
242
248
|| PyBool_Check(obj)
243
#if PY_MAJOR_VERSION >= 3
245
249
|| PyInt_CheckExact(obj)
247
250
|| PyLong_CheckExact(obj)
248
251
|| PyFloat_CheckExact(obj)
249
252
|| PyUnicode_CheckExact(obj)
311
314
if (tuple_repr == NULL) {
314
#if PY_MAJOR_VERSION >= 3
315
result = PyUnicode_FromFormat("StaticTuple%U", tuple_repr);
317
317
result = PyString_FromFormat("StaticTuple%s",
318
318
PyString_AsString(tuple_repr));
323
/* adapted from tuplehash(), is the specific hash value considered
327
#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8)
328
/* Hash for tuples. This is a slightly simplified version of the xxHash
329
non-cryptographic hash:
330
- we do not use any parallellism, there is only 1 accumulator.
331
- we drop the final mixing since this is just a permutation of the
332
output space: it does not help against collisions.
333
- at the end, we mangle the length with a single constant.
334
For the xxHash specification, see
335
https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
337
Below are the official constants from the xxHash specification. Optimizing
338
compilers should emit a single "rotate" instruction for the
339
_PyHASH_XXROTATE() expansion. If that doesn't happen for some important
340
platform, the macro could be changed to expand to a platform-specific rotate
343
#if SIZEOF_PY_UHASH_T > 4
344
#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
345
#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
346
#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
347
#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
349
#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
350
#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
351
#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
352
#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
355
/* Tests have shown that it's not worth to cache the hash value, see
356
https://bugs.python.org/issue9685 */
358
StaticTuple_hash(StaticTuple *self)
360
Py_ssize_t i, len = self->size;
361
PyObject **item = self->items;
363
#if STATIC_TUPLE_HAS_HASH
364
if (self->hash != -1) {
369
Py_uhash_t acc = _PyHASH_XXPRIME_5;
370
for (i = 0; i < len; i++) {
371
Py_uhash_t lane = PyObject_Hash(item[i]);
372
if (lane == (Py_uhash_t)-1) {
375
acc += lane * _PyHASH_XXPRIME_2;
376
acc = _PyHASH_XXROTATE(acc);
377
acc *= _PyHASH_XXPRIME_1;
380
/* Add input length, mangled to keep the historical value of hash(()). */
381
acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
383
if (acc == (Py_uhash_t)-1) {
387
#if STATIC_TUPLE_HAS_HASH
396
323
StaticTuple_hash(StaticTuple *self)
501
427
} else if (w == Py_None) {
502
428
// None is always less than the object
505
#if PY_MAJOR_VERSION >= 3
507
case Py_GT:case Py_GE:
430
case Py_NE:case Py_GT:case Py_GE:
509
431
Py_INCREF(Py_True);
512
#if PY_MAJOR_VERSION >= 3
514
case Py_LT:case Py_LE:
433
case Py_EQ:case Py_LT:case Py_LE:
516
434
Py_INCREF(Py_False);
518
default: // Should only happen on Python 3
519
return Py_NotImplemented;
436
default: // Should never happen
437
return Py_NotImplemented;
522
440
/* We don't special case this comparison, we just let python handle
569
487
/* Shortcut case, these must be identical */
572
if (PyBytes_CheckExact(v_obj) && PyBytes_CheckExact(w_obj)) {
490
if (PyString_CheckExact(v_obj) && PyString_CheckExact(w_obj)) {
573
491
result = string_richcompare(v_obj, w_obj, Py_EQ);
574
492
} else if (StaticTuple_CheckExact(v_obj) &&
575
493
StaticTuple_CheckExact(w_obj))
776
692
Py_DECREF(as_tuple);
782
StaticTuple_subscript(StaticTuple *self, PyObject *key)
784
PyObject *as_tuple, *result;
786
as_tuple = StaticTuple_as_tuple(self);
787
if (as_tuple == NULL) {
790
result = PyTuple_Type.tp_as_mapping->mp_subscript(as_tuple, key);
796
697
StaticTuple_traverse(StaticTuple *self, visitproc visit, void *arg)
807
StaticTuple_sizeof(StaticTuple *self)
811
res = _PyObject_SIZE(&StaticTuple_Type) + (int)self->size * sizeof(void*);
812
return PyInt_FromSsize_t(res);
817
706
static char StaticTuple_doc[] =
818
707
"C implementation of a StaticTuple structure."
819
708
"\n This is used as StaticTuple(item1, item2, item3)"
833
722
"Create a StaticTuple from a given sequence. This functions"
834
723
" the same as the tuple() constructor."},
835
724
{"__reduce__", (PyCFunction)StaticTuple_reduce, METH_NOARGS, StaticTuple_reduce_doc},
836
{"__sizeof__", (PyCFunction)StaticTuple_sizeof, METH_NOARGS},
837
725
{NULL, NULL} /* sentinel */
859
747
0, /* nb_coerce */
863
751
static PySequenceMethods StaticTuple_as_sequence = {
864
752
(lenfunc)StaticTuple_length, /* sq_length */
865
753
0, /* sq_concat */
866
754
0, /* sq_repeat */
867
755
(ssizeargfunc)StaticTuple_item, /* sq_item */
868
#if PY_MAJOR_VERSION >= 3
870
756
(ssizessizeargfunc)StaticTuple_slice, /* sq_slice */
872
757
0, /* sq_ass_item */
873
758
0, /* sq_ass_slice */
874
759
0, /* sq_contains */
875
#if PY_MAJOR_VERSION >= 3
876
0, /* sq_inplace_concat */
877
0, /* sq_inplace_repeat */
882
static PyMappingMethods StaticTuple_as_mapping = {
883
(lenfunc)StaticTuple_length, /* mp_length */
884
(binaryfunc)StaticTuple_subscript, /* mp_subscript */
885
0, /* mp_ass_subscript */
762
/* TODO: Implement StaticTuple_as_mapping.
763
* The only thing we really want to support from there is mp_subscript,
764
* so that we could support extended slicing (foo[::2]). Not worth it
889
769
PyTypeObject StaticTuple_Type = {
890
PyVarObject_HEAD_INIT(NULL, 0)
891
"breezy._static_tuple_c.StaticTuple", /* tp_name */
770
PyObject_HEAD_INIT(NULL)
772
"bzrlib._static_tuple_c.StaticTuple", /* tp_name */
892
773
sizeof(StaticTuple), /* tp_basicsize */
893
774
sizeof(PyObject *), /* tp_itemsize */
894
775
(destructor)StaticTuple_dealloc, /* tp_dealloc */
899
780
(reprfunc)StaticTuple_repr, /* tp_repr */
900
781
&StaticTuple_as_number, /* tp_as_number */
901
782
&StaticTuple_as_sequence, /* tp_as_sequence */
902
&StaticTuple_as_mapping, /* tp_as_mapping */
783
0, /* tp_as_mapping */
903
784
(hashfunc)StaticTuple_hash, /* tp_hash */
996
PYMOD_INIT_FUNC(_static_tuple_c)
878
_workaround_pyrex_096(void)
880
/* Work around an incompatibility in how pyrex 0.9.6 exports a module,
881
* versus how pyrex 0.9.8 and cython 0.11 export it.
882
* Namely 0.9.6 exports import__simple_set_pyx and tries to
883
* "import _simple_set_pyx" but it is available only as
884
* "import bzrlib._simple_set_pyx"
885
* It is a shame to hack up sys.modules, but that is what we've got to do.
887
PyObject *sys_module = NULL, *modules = NULL, *set_module = NULL;
890
/* Clear out the current ImportError exception, and try again. */
892
/* Note that this only seems to work if somewhere else imports
893
* bzrlib._simple_set_pyx before importing bzrlib._static_tuple_c
895
set_module = PyImport_ImportModule("bzrlib._simple_set_pyx");
896
if (set_module == NULL) {
899
/* Add the _simple_set_pyx into sys.modules at the appropriate location. */
900
sys_module = PyImport_ImportModule("sys");
901
if (sys_module == NULL) {
904
modules = PyObject_GetAttrString(sys_module, "modules");
905
if (modules == NULL || !PyDict_Check(modules)) {
908
PyDict_SetItemString(modules, "_simple_set_pyx", set_module);
909
/* Now that we have hacked it in, try the import again. */
910
retval = import_bzrlib___simple_set_pyx();
912
Py_XDECREF(set_module);
913
Py_XDECREF(sys_module);
920
init_static_tuple_c(void)
1000
924
StaticTuple_Type.tp_getattro = PyObject_GenericGetAttr;
1001
if (PyType_Ready(&StaticTuple_Type) < 0) {
925
if (PyType_Ready(&StaticTuple_Type) < 0)
1005
PYMOD_CREATE(m, "_static_tuple_c",
1006
"C implementation of a StaticTuple structure",
1007
static_tuple_c_methods);
928
m = Py_InitModule3("_static_tuple_c", static_tuple_c_methods,
929
"C implementation of a StaticTuple structure");
1012
933
Py_INCREF(&StaticTuple_Type);
1013
934
PyModule_AddObject(m, "StaticTuple", (PyObject *)&StaticTuple_Type);
1014
if (import_breezy___simple_set_pyx() == -1) {
935
if (import_bzrlib___simple_set_pyx() == -1
936
&& _workaround_pyrex_096() == -1)
1017
940
setup_interned_tuples(m);
1018
941
setup_empty_tuple(m);
1021
return PYMOD_SUCCESS(m);
1024
945
// vim: tabstop=4 sw=4 expandtab