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_breezy___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_breezy___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));
427
428
} else if (w == Py_None) {
428
429
// None is always less than the object
430
case Py_NE:case Py_GT:case Py_GE:
432
#if PY_MAJOR_VERSION >= 3
434
case Py_GT:case Py_GE:
431
436
Py_INCREF(Py_True);
433
case Py_EQ:case Py_LT:case Py_LE:
439
#if PY_MAJOR_VERSION >= 3
441
case Py_LT:case Py_LE:
434
443
Py_INCREF(Py_False);
436
default: // Should never happen
437
return Py_NotImplemented;
445
default: // Should only happen on Python 3
446
return Py_NotImplemented;
440
449
/* We don't special case this comparison, we just let python handle
487
496
/* Shortcut case, these must be identical */
490
if (PyString_CheckExact(v_obj) && PyString_CheckExact(w_obj)) {
499
if (PyBytes_CheckExact(v_obj) && PyBytes_CheckExact(w_obj)) {
491
500
result = string_richcompare(v_obj, w_obj, Py_EQ);
492
501
} else if (StaticTuple_CheckExact(v_obj) &&
493
502
StaticTuple_CheckExact(w_obj))
549
558
/* It is some other comparison, go ahead and do the real check. */
550
if (PyString_CheckExact(v_obj) && PyString_CheckExact(w_obj))
559
if (PyBytes_CheckExact(v_obj) && PyBytes_CheckExact(w_obj))
552
561
return string_richcompare(v_obj, w_obj, op);
553
562
} else if (StaticTuple_CheckExact(v_obj) &&
692
703
Py_DECREF(as_tuple);
709
StaticTuple_subscript(StaticTuple *self, PyObject *key)
711
PyObject *as_tuple, *result;
713
as_tuple = StaticTuple_as_tuple(self);
714
if (as_tuple == NULL) {
717
result = PyTuple_Type.tp_as_mapping->mp_subscript(as_tuple, key);
697
723
StaticTuple_traverse(StaticTuple *self, visitproc visit, void *arg)
760
786
0, /* nb_coerce */
764
790
static PySequenceMethods StaticTuple_as_sequence = {
765
791
(lenfunc)StaticTuple_length, /* sq_length */
766
792
0, /* sq_concat */
767
793
0, /* sq_repeat */
768
794
(ssizeargfunc)StaticTuple_item, /* sq_item */
795
#if PY_MAJOR_VERSION >= 3
769
797
(ssizessizeargfunc)StaticTuple_slice, /* sq_slice */
770
799
0, /* sq_ass_item */
771
800
0, /* sq_ass_slice */
772
801
0, /* sq_contains */
775
/* TODO: Implement StaticTuple_as_mapping.
776
* The only thing we really want to support from there is mp_subscript,
777
* so that we could support extended slicing (foo[::2]). Not worth it
802
#if PY_MAJOR_VERSION >= 3
803
0, /* sq_inplace_concat */
804
0, /* sq_inplace_repeat */
809
static PyMappingMethods StaticTuple_as_mapping = {
810
(lenfunc)StaticTuple_length, /* mp_length */
811
(binaryfunc)StaticTuple_subscript, /* mp_subscript */
812
0, /* mp_ass_subscript */
782
816
PyTypeObject StaticTuple_Type = {
783
PyObject_HEAD_INIT(NULL)
817
PyVarObject_HEAD_INIT(NULL, 0)
785
818
"breezy._static_tuple_c.StaticTuple", /* tp_name */
786
819
sizeof(StaticTuple), /* tp_basicsize */
787
820
sizeof(PyObject *), /* tp_itemsize */
793
826
(reprfunc)StaticTuple_repr, /* tp_repr */
794
827
&StaticTuple_as_number, /* tp_as_number */
795
828
&StaticTuple_as_sequence, /* tp_as_sequence */
796
0, /* tp_as_mapping */
829
&StaticTuple_as_mapping, /* tp_as_mapping */
797
830
(hashfunc)StaticTuple_hash, /* tp_hash */
891
_workaround_pyrex_096(void)
893
/* Work around an incompatibility in how pyrex 0.9.6 exports a module,
894
* versus how pyrex 0.9.8 and cython 0.11 export it.
895
* Namely 0.9.6 exports import__simple_set_pyx and tries to
896
* "import _simple_set_pyx" but it is available only as
897
* "import breezy._simple_set_pyx"
898
* It is a shame to hack up sys.modules, but that is what we've got to do.
900
PyObject *sys_module = NULL, *modules = NULL, *set_module = NULL;
903
/* Clear out the current ImportError exception, and try again. */
905
/* Note that this only seems to work if somewhere else imports
906
* breezy._simple_set_pyx before importing breezy._static_tuple_c
908
set_module = PyImport_ImportModule("breezy._simple_set_pyx");
909
if (set_module == NULL) {
912
/* Add the _simple_set_pyx into sys.modules at the appropriate location. */
913
sys_module = PyImport_ImportModule("sys");
914
if (sys_module == NULL) {
917
modules = PyObject_GetAttrString(sys_module, "modules");
918
if (modules == NULL || !PyDict_Check(modules)) {
921
PyDict_SetItemString(modules, "_simple_set_pyx", set_module);
922
/* Now that we have hacked it in, try the import again. */
923
retval = import_breezy___simple_set_pyx();
925
Py_XDECREF(set_module);
926
Py_XDECREF(sys_module);
933
init_static_tuple_c(void)
923
PYMOD_INIT_FUNC(_static_tuple_c)
937
927
StaticTuple_Type.tp_getattro = PyObject_GenericGetAttr;
938
if (PyType_Ready(&StaticTuple_Type) < 0)
928
if (PyType_Ready(&StaticTuple_Type) < 0) {
941
m = Py_InitModule3("_static_tuple_c", static_tuple_c_methods,
942
"C implementation of a StaticTuple structure");
932
PYMOD_CREATE(m, "_static_tuple_c",
933
"C implementation of a StaticTuple structure",
934
static_tuple_c_methods);
946
939
Py_INCREF(&StaticTuple_Type);
947
940
PyModule_AddObject(m, "StaticTuple", (PyObject *)&StaticTuple_Type);
948
if (import_breezy___simple_set_pyx() == -1
949
&& _workaround_pyrex_096() == -1)
941
if (import_breezy___simple_set_pyx() == -1) {
953
944
setup_interned_tuples(m);
954
945
setup_empty_tuple(m);
948
return PYMOD_SUCCESS(m);
958
951
// vim: tabstop=4 sw=4 expandtab