~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_chk_map_pyx.pyx

  • Committer: John Arbash Meinel
  • Date: 2009-10-21 19:43:05 UTC
  • mto: This revision was merged to the branch mainline in revision 4771.
  • Revision ID: john@arbash-meinel.com-20091021194305-eso7vfaetsvmhgz6
Tweak some of the internals of _chk_map_pyx.pyx

Since we have a fixed type, we can use the StaticTuple_GET_ITEM macro.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
cdef extern from "Python.h":
31
31
    ctypedef int Py_ssize_t # Required for older pyrex versions
32
 
    struct _PyObject:
 
32
    ctypedef struct PyObject:
33
33
        pass
34
 
    ctypedef _PyObject PyObject
35
34
    int PyTuple_CheckExact(object p)
36
35
    Py_ssize_t PyTuple_GET_SIZE(object t)
37
36
    int PyString_CheckExact(object)
52
51
    char *PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *s)
53
52
    object PyString_FromStringAndSize(char*, Py_ssize_t)
54
53
 
 
54
# It seems we need to import the definitions so that the pyrex compiler has
 
55
# local names to access them.
 
56
from _static_tuple_c cimport StaticTuple,\
 
57
    import_static_tuple_c, StaticTuple_New, \
 
58
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
 
59
 
 
60
cdef extern from "_static_tuple_c.h":
 
61
    # Defined explicitly rathert than cimport ing. Using cimport the type for
 
62
    # PyObject is a different class that happens to have the same name...
 
63
    PyObject * StaticTuple_GET_ITEM_ptr "StaticTuple_GET_ITEM" (StaticTuple,
 
64
                                                                Py_ssize_t)
 
65
 
55
66
cdef extern from "zlib.h":
56
67
    ctypedef unsigned long uLong
57
68
    ctypedef unsigned int uInt
59
70
 
60
71
    uLong crc32(uLong crc, Bytef *buf, uInt len)
61
72
 
62
 
# It seems we need to import the definitions so that the pyrex compiler has
63
 
# local names to access them.
64
 
from _static_tuple_c cimport StaticTuple,\
65
 
    import_static_tuple_c, StaticTuple_New, \
66
 
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
67
73
 
68
74
 
69
75
# This sets up the StaticTuple C_API functionality
101
107
    cdef uInt crc_val
102
108
    cdef Py_ssize_t out_off
103
109
    cdef char *c_out
104
 
    # cdef PyObject *bit
 
110
    cdef PyObject *bit
105
111
 
106
112
    if not StaticTuple_CheckExact(key):
107
113
        raise TypeError('key %r is not a StaticTuple' % (key,))
117
123
        # We use the _ptr variant, because GET_ITEM returns a borrowed
118
124
        # reference, and Pyrex assumes that returned 'object' are a new
119
125
        # reference
120
 
        # XXX: This needs to be updated for PySequence_GetItem since both
121
 
        #      PyTuple and StaticTuple support that api
122
 
        bit = key[i]# PyTuple_GET_ITEM_ptr(key, i)
123
 
        if not PyString_CheckExact(bit):
 
126
        bit = StaticTuple_GET_ITEM_ptr(key, i)
 
127
        if not PyString_CheckExact_ptr(bit):
124
128
            raise TypeError('Bit %d of %r is not a string' % (i, key))
125
 
        c_bit = <Bytef *>PyString_AS_STRING(bit)
126
 
        c_len = PyString_GET_SIZE(bit)
 
129
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
 
130
        c_len = PyString_GET_SIZE_ptr(bit)
127
131
        crc_val = crc32(0, c_bit, c_len)
128
132
        # Hex(val) order
129
133
        sprintf(c_out, '%08X', crc_val)
141
145
    cdef uInt crc_val
142
146
    cdef Py_ssize_t out_off
143
147
    cdef char *c_out
144
 
    # cdef PyObject *bit
 
148
    cdef PyObject *bit
145
149
 
146
150
    if not StaticTuple_CheckExact(key):
147
151
        raise TypeError('key %r is not a StaticTuple' % (key,))
154
158
        if i > 0:
155
159
            c_out[0] = c'\x00'
156
160
            c_out = c_out + 1
157
 
        bit = key[i] # PyTuple_GET_ITEM_ptr(key, i)
158
 
        if not PyString_CheckExact(bit):
159
 
            raise TypeError('Bit %d of %r is not a string: %r' % (i, key,
160
 
            bit))
161
 
        c_bit = <Bytef *>PyString_AS_STRING(bit)
162
 
        c_len = PyString_GET_SIZE(bit)
 
161
        bit = StaticTuple_GET_ITEM_ptr(key, i)
 
162
        if not PyString_CheckExact_ptr(bit):
 
163
            raise TypeError('Bit %d of %r is not a string: %r'
 
164
                            % (i, key, <object>bit))
 
165
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
 
166
        c_len = PyString_GET_SIZE_ptr(bit)
163
167
        crc_val = crc32(0, c_bit, c_len)
164
168
        # MSB order
165
169
        c_out[0] = (crc_val >> 24) & 0xFF