/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 bzrlib/_simple_set_pyx.pyx

  • Committer: John Arbash Meinel
  • Date: 2009-10-12 21:44:27 UTC
  • mto: This revision was merged to the branch mainline in revision 4737.
  • Revision ID: john@arbash-meinel.com-20091012214427-zddi1kmc2jlf7v31
Py_ssize_t and its associated function typedefs are not available w/ python 2.4

So we define them in python-compat.h
Even further, gcc issued a warning for:
static int
_workaround_pyrex_096()
So we changed it to:
_workaround_pyrex_096(void)

Also, some python api funcs were incorrectly defined as 'char *' when they meant
'const char *'. Work around that with a (char *) cast, to avoid compiler warnings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
21
21
 
22
22
cdef extern from "Python.h":
23
23
    ctypedef unsigned long size_t
24
 
    ctypedef long (*hashfunc)(PyObject*) except -1
25
 
    ctypedef object (*richcmpfunc)(PyObject *, PyObject *, int)
 
24
    ctypedef long (*hashfunc)(PyObject*)
 
25
    ctypedef PyObject *(*richcmpfunc)(PyObject *, PyObject *, int)
26
26
    ctypedef int (*visitproc)(PyObject *, void *)
27
27
    ctypedef int (*traverseproc)(PyObject *, visitproc, void *)
28
28
    int Py_EQ
 
29
    PyObject *Py_True
 
30
    PyObject *Py_NotImplemented
29
31
    void Py_INCREF(PyObject *)
30
32
    void Py_DECREF(PyObject *)
31
33
    ctypedef struct PyTypeObject:
34
36
        traverseproc tp_traverse
35
37
 
36
38
    PyTypeObject *Py_TYPE(PyObject *)
37
 
    # Note: *Don't* use hash(), Pyrex 0.9.8.5 thinks it returns an 'int', and
38
 
    #       thus silently truncates to 32-bits on 64-bit machines.
39
 
    long PyObject_Hash(PyObject *) except -1
 
39
    int PyObject_IsTrue(PyObject *)
40
40
        
41
41
    void *PyMem_Malloc(size_t nbytes)
42
42
    void PyMem_Free(void *)
55
55
_dummy = <PyObject *>_dummy_obj
56
56
 
57
57
 
58
 
cdef object _NotImplemented
59
 
_NotImplemented = NotImplemented
60
 
 
61
 
 
62
58
cdef int _is_equal(PyObject *this, long this_hash, PyObject *other) except -1:
63
59
    cdef long other_hash
 
60
    cdef PyObject *res
64
61
 
65
62
    if this == other:
66
63
        return 1
67
 
    other_hash = PyObject_Hash(other)
 
64
    other_hash = Py_TYPE(other).tp_hash(other)
 
65
    if other_hash == -1:
 
66
        # Even though other successfully hashed in the past, it seems to have
 
67
        # changed its mind, and failed this time, so propogate the failure.
 
68
        return -1
68
69
    if other_hash != this_hash:
69
70
        return 0
70
71
 
76
77
    #      equal. (It doesn't try to cast them both to some intermediate form
77
78
    #      that would compare equal.)
78
79
    res = Py_TYPE(this).tp_richcompare(this, other, Py_EQ)
79
 
    if res is _NotImplemented:
 
80
    if res == NULL: # Exception
 
81
        return -1
 
82
    if PyObject_IsTrue(res):
 
83
        Py_DECREF(res)
 
84
        return 1
 
85
    if res == Py_NotImplemented:
 
86
        Py_DECREF(res)
80
87
        res = Py_TYPE(other).tp_richcompare(other, this, Py_EQ)
81
 
        if res is _NotImplemented:
82
 
            return 0
83
 
    if res:
 
88
    if res == NULL:
 
89
        return -1
 
90
    if PyObject_IsTrue(res):
 
91
        Py_DECREF(res)
84
92
        return 1
 
93
    Py_DECREF(res)
85
94
    return 0
86
95
 
87
96
 
194
203
        mask = self._mask
195
204
        table = self._table
196
205
 
197
 
        the_hash = PyObject_Hash(key)
 
206
        the_hash = Py_TYPE(key).tp_hash(key)
 
207
        if the_hash == -1:
 
208
            return -1
198
209
        i = the_hash
199
210
        for n_lookup from 0 <= n_lookup <= <size_t>mask: # Don't loop forever
200
211
            slot = &table[i & mask]
447
458
    cdef long key_hash
448
459
    cdef PyObject **table, **slot, *cur, **free_slot, *py_key
449
460
 
450
 
    py_key = <PyObject *>key
451
 
    # Note: avoid using hash(obj) because of a bug w/ pyrex 0.9.8.5 and 64-bit
452
 
    #       (it treats hash() as returning an 'int' rather than a 'long')
453
 
    key_hash = PyObject_Hash(py_key)
 
461
    # hash is a signed long(), we are using an offset at unsigned size_t
 
462
    key_hash = hash(key)
454
463
    i = <size_t>key_hash
455
464
    mask = self._mask
456
465
    table = self._table
457
466
    free_slot = NULL
 
467
    py_key = <PyObject *>key
458
468
    for n_lookup from 0 <= n_lookup <= <size_t>mask: # Don't loop forever
459
469
        slot = &table[i & mask]
460
470
        cur = slot[0]
540
550
    return _check_self(self)._used
541
551
 
542
552
 
543
 
cdef api int SimpleSet_Next(object self, Py_ssize_t *pos,
544
 
                            PyObject **key) except -1:
 
553
cdef api int SimpleSet_Next(object self, Py_ssize_t *pos, PyObject **key):
545
554
    """Walk over items in a SimpleSet.
546
555
 
547
556
    :param pos: should be initialized to 0 by the caller, and will be updated
568
577
    return 1
569
578
 
570
579
 
571
 
cdef int SimpleSet_traverse(SimpleSet self, visitproc visit,
572
 
                            void *arg) except -1:
 
580
cdef int SimpleSet_traverse(SimpleSet self, visitproc visit, void *arg):
573
581
    """This is an implementation of 'tp_traverse' that hits the whole table.
574
582
 
575
583
    Cython/Pyrex don't seem to let you define a tp_traverse, and they only