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
35
29
#include "_simple_set_pyx_api.h"
37
31
#if defined(__GNUC__)
242
236
" should not have a NULL entry.");
245
if (PyString_CheckExact(obj)
239
if (PyBytes_CheckExact(obj)
246
240
|| StaticTuple_CheckExact(obj)
247
241
|| obj == Py_None
248
242
|| PyBool_Check(obj)
243
#if PY_MAJOR_VERSION >= 3
249
245
|| PyInt_CheckExact(obj)
250
247
|| PyLong_CheckExact(obj)
251
248
|| PyFloat_CheckExact(obj)
252
249
|| PyUnicode_CheckExact(obj)
314
311
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));
487
488
/* Shortcut case, these must be identical */
490
if (PyString_CheckExact(v_obj) && PyString_CheckExact(w_obj)) {
491
if (PyBytes_CheckExact(v_obj) && PyBytes_CheckExact(w_obj)) {
491
492
result = string_richcompare(v_obj, w_obj, Py_EQ);
492
493
} else if (StaticTuple_CheckExact(v_obj) &&
493
494
StaticTuple_CheckExact(w_obj))
549
550
/* It is some other comparison, go ahead and do the real check. */
550
if (PyString_CheckExact(v_obj) && PyString_CheckExact(w_obj))
551
if (PyBytes_CheckExact(v_obj) && PyBytes_CheckExact(w_obj))
552
553
return string_richcompare(v_obj, w_obj, op);
553
554
} else if (StaticTuple_CheckExact(v_obj) &&
692
695
Py_DECREF(as_tuple);
701
StaticTuple_subscript(StaticTuple *self, PyObject *key)
703
PyObject *as_tuple, *result;
705
as_tuple = StaticTuple_as_tuple(self);
706
if (as_tuple == NULL) {
709
result = PyTuple_Type.tp_as_mapping->mp_subscript(as_tuple, key);
697
715
StaticTuple_traverse(StaticTuple *self, visitproc visit, void *arg)
726
StaticTuple_sizeof(StaticTuple *self)
730
res = _PyObject_SIZE(&StaticTuple_Type) + (int)self->size * sizeof(void*);
731
return PyInt_FromSsize_t(res);
706
736
static char StaticTuple_doc[] =
707
737
"C implementation of a StaticTuple structure."
708
738
"\n This is used as StaticTuple(item1, item2, item3)"
722
752
"Create a StaticTuple from a given sequence. This functions"
723
753
" the same as the tuple() constructor."},
724
754
{"__reduce__", (PyCFunction)StaticTuple_reduce, METH_NOARGS, StaticTuple_reduce_doc},
755
{"__sizeof__", (PyCFunction)StaticTuple_sizeof, METH_NOARGS},
725
756
{NULL, NULL} /* sentinel */
747
778
0, /* nb_coerce */
751
782
static PySequenceMethods StaticTuple_as_sequence = {
752
783
(lenfunc)StaticTuple_length, /* sq_length */
753
784
0, /* sq_concat */
754
785
0, /* sq_repeat */
755
786
(ssizeargfunc)StaticTuple_item, /* sq_item */
787
#if PY_MAJOR_VERSION >= 3
756
789
(ssizessizeargfunc)StaticTuple_slice, /* sq_slice */
757
791
0, /* sq_ass_item */
758
792
0, /* sq_ass_slice */
759
793
0, /* sq_contains */
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
794
#if PY_MAJOR_VERSION >= 3
795
0, /* sq_inplace_concat */
796
0, /* sq_inplace_repeat */
801
static PyMappingMethods StaticTuple_as_mapping = {
802
(lenfunc)StaticTuple_length, /* mp_length */
803
(binaryfunc)StaticTuple_subscript, /* mp_subscript */
804
0, /* mp_ass_subscript */
769
808
PyTypeObject StaticTuple_Type = {
770
PyObject_HEAD_INIT(NULL)
772
"bzrlib._static_tuple_c.StaticTuple", /* tp_name */
809
PyVarObject_HEAD_INIT(NULL, 0)
810
"breezy._static_tuple_c.StaticTuple", /* tp_name */
773
811
sizeof(StaticTuple), /* tp_basicsize */
774
812
sizeof(PyObject *), /* tp_itemsize */
775
813
(destructor)StaticTuple_dealloc, /* tp_dealloc */
780
818
(reprfunc)StaticTuple_repr, /* tp_repr */
781
819
&StaticTuple_as_number, /* tp_as_number */
782
820
&StaticTuple_as_sequence, /* tp_as_sequence */
783
0, /* tp_as_mapping */
821
&StaticTuple_as_mapping, /* tp_as_mapping */
784
822
(hashfunc)StaticTuple_hash, /* tp_hash */
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)
915
PYMOD_INIT_FUNC(_static_tuple_c)
924
919
StaticTuple_Type.tp_getattro = PyObject_GenericGetAttr;
925
if (PyType_Ready(&StaticTuple_Type) < 0)
920
if (PyType_Ready(&StaticTuple_Type) < 0) {
928
m = Py_InitModule3("_static_tuple_c", static_tuple_c_methods,
929
"C implementation of a StaticTuple structure");
924
PYMOD_CREATE(m, "_static_tuple_c",
925
"C implementation of a StaticTuple structure",
926
static_tuple_c_methods);
933
931
Py_INCREF(&StaticTuple_Type);
934
932
PyModule_AddObject(m, "StaticTuple", (PyObject *)&StaticTuple_Type);
935
if (import_bzrlib___simple_set_pyx() == -1
936
&& _workaround_pyrex_096() == -1)
933
if (import_breezy___simple_set_pyx() == -1) {
940
936
setup_interned_tuples(m);
941
937
setup_empty_tuple(m);
940
return PYMOD_SUCCESS(m);
945
943
// vim: tabstop=4 sw=4 expandtab