/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/_static_tuple_c.c

  • Committer: Jelmer Vernooij
  • Date: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include "_static_tuple_c.h"
27
27
#include "_export_c_api.h"
28
28
 
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.
33
 
 */
34
 
#define import__simple_set_pyx import_bzrlib___simple_set_pyx
35
29
#include "_simple_set_pyx_api.h"
36
30
 
37
31
#if defined(__GNUC__)
242
236
                " should not have a NULL entry.");
243
237
            return 0;
244
238
        }
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
 
244
#else
249
245
            || PyInt_CheckExact(obj)
 
246
#endif
250
247
            || PyLong_CheckExact(obj)
251
248
            || PyFloat_CheckExact(obj)
252
249
            || PyUnicode_CheckExact(obj)
314
311
    if (tuple_repr == NULL) {
315
312
        return NULL;
316
313
    }
 
314
#if PY_MAJOR_VERSION >= 3
 
315
    result = PyUnicode_FromFormat("StaticTuple%U", tuple_repr);
 
316
#else
317
317
    result = PyString_FromFormat("StaticTuple%s",
318
318
                                 PyString_AsString(tuple_repr));
 
319
#endif
319
320
    return result;
320
321
}
321
322
 
362
363
{
363
364
    PyObject *vt;
364
365
    PyObject *result = NULL;
365
 
    
 
366
 
366
367
    vt = StaticTuple_as_tuple((StaticTuple *)v);
367
368
    if (vt == NULL) {
368
369
        goto done;
427
428
    } else if (w == Py_None) {
428
429
        // None is always less than the object
429
430
        switch (op) {
430
 
        case Py_NE:case Py_GT:case Py_GE:
 
431
        case Py_NE:
 
432
#if PY_MAJOR_VERSION >= 3
 
433
#else
 
434
        case Py_GT:case Py_GE:
 
435
#endif
431
436
            Py_INCREF(Py_True);
432
437
            return Py_True;
433
 
        case Py_EQ:case Py_LT:case Py_LE:
 
438
        case Py_EQ:
 
439
#if PY_MAJOR_VERSION >= 3
 
440
#else
 
441
        case Py_LT:case Py_LE:
 
442
#endif
434
443
            Py_INCREF(Py_False);
435
444
            return Py_False;
436
 
    default: // Should never happen
437
 
        return Py_NotImplemented;
 
445
        default: // Should only happen on Python 3
 
446
            return Py_NotImplemented;
438
447
        }
439
448
    } else {
440
449
        /* We don't special case this comparison, we just let python handle
478
487
    vlen = v_st->size;
479
488
    wlen = w_st->size;
480
489
    min_len = (vlen < wlen) ? vlen : wlen;
481
 
    string_richcompare = PyString_Type.tp_richcompare;
 
490
    string_richcompare = PyBytes_Type.tp_richcompare;
482
491
    for (i = 0; i < min_len; i++) {
483
492
        PyObject *result = NULL;
484
493
        v_obj = StaticTuple_GET_ITEM(v_st, i);
487
496
            /* Shortcut case, these must be identical */
488
497
            continue;
489
498
        }
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))
547
556
        return Py_True;
548
557
    }
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))
551
560
    {
552
561
        return string_richcompare(v_obj, w_obj, op);
553
562
    } else if (StaticTuple_CheckExact(v_obj) &&
679
688
    return obj;
680
689
}
681
690
 
 
691
#if PY_MAJOR_VERSION >= 3
 
692
#else
682
693
static PyObject *
683
694
StaticTuple_slice(StaticTuple *self, Py_ssize_t ilow, Py_ssize_t ihigh)
684
695
{
692
703
    Py_DECREF(as_tuple);
693
704
    return result;
694
705
}
 
706
#endif
 
707
 
 
708
static PyObject *
 
709
StaticTuple_subscript(StaticTuple *self, PyObject *key)
 
710
{
 
711
    PyObject *as_tuple, *result;
 
712
 
 
713
    as_tuple = StaticTuple_as_tuple(self);
 
714
    if (as_tuple == NULL) {
 
715
        return NULL;
 
716
    }
 
717
    result = PyTuple_Type.tp_as_mapping->mp_subscript(as_tuple, key);
 
718
    Py_DECREF(as_tuple);
 
719
    return result;
 
720
}
695
721
 
696
722
static int
697
723
StaticTuple_traverse(StaticTuple *self, visitproc visit, void *arg)
703
729
    return 0;
704
730
}
705
731
 
 
732
 
 
733
static PyObject *
 
734
StaticTuple_sizeof(StaticTuple *self)
 
735
{
 
736
    Py_ssize_t res;
 
737
 
 
738
    res = _PyObject_SIZE(&StaticTuple_Type) + (int)self->size * sizeof(void*);
 
739
    return PyInt_FromSsize_t(res);
 
740
}
 
741
 
 
742
 
 
743
 
706
744
static char StaticTuple_doc[] =
707
745
    "C implementation of a StaticTuple structure."
708
746
    "\n This is used as StaticTuple(item1, item2, item3)"
722
760
     "Create a StaticTuple from a given sequence. This functions"
723
761
     " the same as the tuple() constructor."},
724
762
    {"__reduce__", (PyCFunction)StaticTuple_reduce, METH_NOARGS, StaticTuple_reduce_doc},
 
763
    {"__sizeof__",  (PyCFunction)StaticTuple_sizeof, METH_NOARGS}, 
725
764
    {NULL, NULL} /* sentinel */
726
765
};
727
766
 
746
785
    0,                              /* nb_or */
747
786
    0,                              /* nb_coerce */
748
787
};
749
 
    
 
788
 
750
789
 
751
790
static PySequenceMethods StaticTuple_as_sequence = {
752
791
    (lenfunc)StaticTuple_length,            /* sq_length */
753
792
    0,                              /* sq_concat */
754
793
    0,                              /* sq_repeat */
755
794
    (ssizeargfunc)StaticTuple_item,         /* sq_item */
 
795
#if PY_MAJOR_VERSION >= 3
 
796
#else
756
797
    (ssizessizeargfunc)StaticTuple_slice,   /* sq_slice */
 
798
#endif
757
799
    0,                              /* sq_ass_item */
758
800
    0,                              /* sq_ass_slice */
759
801
    0,                              /* sq_contains */
760
 
};
761
 
 
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
765
 
 *       yet, though.
766
 
 */
 
802
#if PY_MAJOR_VERSION >= 3
 
803
    0,                              /* sq_inplace_concat */
 
804
    0,                              /* sq_inplace_repeat */
 
805
#endif
 
806
};
 
807
 
 
808
 
 
809
static PyMappingMethods StaticTuple_as_mapping = {
 
810
    (lenfunc)StaticTuple_length,            /* mp_length */
 
811
    (binaryfunc)StaticTuple_subscript,      /* mp_subscript */
 
812
    0,                                      /* mp_ass_subscript */
 
813
};
767
814
 
768
815
 
769
816
PyTypeObject StaticTuple_Type = {
770
 
    PyObject_HEAD_INIT(NULL)
771
 
    0,                                           /* ob_size */
772
 
    "bzrlib._static_tuple_c.StaticTuple",        /* tp_name */
 
817
    PyVarObject_HEAD_INIT(NULL, 0)
 
818
    "breezy._static_tuple_c.StaticTuple",        /* tp_name */
773
819
    sizeof(StaticTuple),                         /* tp_basicsize */
774
820
    sizeof(PyObject *),                          /* tp_itemsize */
775
821
    (destructor)StaticTuple_dealloc,             /* tp_dealloc */
780
826
    (reprfunc)StaticTuple_repr,                  /* tp_repr */
781
827
    &StaticTuple_as_number,                      /* tp_as_number */
782
828
    &StaticTuple_as_sequence,                    /* tp_as_sequence */
783
 
    0,                                           /* tp_as_mapping */
 
829
    &StaticTuple_as_mapping,                     /* tp_as_mapping */
784
830
    (hashfunc)StaticTuple_hash,                  /* tp_hash */
785
831
    0,                                           /* tp_call */
786
832
    0,                                           /* tp_str */
874
920
}
875
921
 
876
922
 
877
 
static int
878
 
_workaround_pyrex_096(void)
879
 
{
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.
886
 
     */
887
 
    PyObject *sys_module = NULL, *modules = NULL, *set_module = NULL;
888
 
    int retval = -1;
889
 
 
890
 
    /* Clear out the current ImportError exception, and try again. */
891
 
    PyErr_Clear();
892
 
    /* Note that this only seems to work if somewhere else imports
893
 
     * bzrlib._simple_set_pyx before importing bzrlib._static_tuple_c
894
 
     */
895
 
    set_module = PyImport_ImportModule("bzrlib._simple_set_pyx");
896
 
    if (set_module == NULL) {
897
 
        goto end;
898
 
    }
899
 
    /* Add the _simple_set_pyx into sys.modules at the appropriate location. */
900
 
    sys_module = PyImport_ImportModule("sys");
901
 
    if (sys_module == NULL) {
902
 
        goto end;
903
 
    }
904
 
    modules = PyObject_GetAttrString(sys_module, "modules");
905
 
    if (modules == NULL || !PyDict_Check(modules)) {
906
 
        goto end;
907
 
    }
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();
911
 
end:
912
 
    Py_XDECREF(set_module);
913
 
    Py_XDECREF(sys_module);
914
 
    Py_XDECREF(modules);
915
 
    return retval;
916
 
}
917
 
 
918
 
 
919
 
PyMODINIT_FUNC
920
 
init_static_tuple_c(void)
 
923
PYMOD_INIT_FUNC(_static_tuple_c)
921
924
{
922
925
    PyObject* m;
923
926
 
924
927
    StaticTuple_Type.tp_getattro = PyObject_GenericGetAttr;
925
 
    if (PyType_Ready(&StaticTuple_Type) < 0)
926
 
        return;
 
928
    if (PyType_Ready(&StaticTuple_Type) < 0) {
 
929
        return PYMOD_ERROR;
 
930
    }
927
931
 
928
 
    m = Py_InitModule3("_static_tuple_c", static_tuple_c_methods,
929
 
                       "C implementation of a StaticTuple structure");
930
 
    if (m == NULL)
931
 
      return;
 
932
    PYMOD_CREATE(m, "_static_tuple_c",
 
933
                 "C implementation of a StaticTuple structure",
 
934
                 static_tuple_c_methods);
 
935
    if (m == NULL) {
 
936
      return PYMOD_ERROR;
 
937
    }
932
938
 
933
939
    Py_INCREF(&StaticTuple_Type);
934
940
    PyModule_AddObject(m, "StaticTuple", (PyObject *)&StaticTuple_Type);
935
 
    if (import_bzrlib___simple_set_pyx() == -1
936
 
        && _workaround_pyrex_096() == -1)
937
 
    {
938
 
        return;
 
941
    if (import_breezy___simple_set_pyx() == -1) {
 
942
        return PYMOD_ERROR;
939
943
    }
940
944
    setup_interned_tuples(m);
941
945
    setup_empty_tuple(m);
942
946
    setup_c_api(m);
 
947
 
 
948
    return PYMOD_SUCCESS(m);
943
949
}
944
950
 
945
951
// vim: tabstop=4 sw=4 expandtab