~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: 2010-05-11 14:13:31 UTC
  • mto: (5218.2.2 bytes_to_entry_c)
  • mto: This revision was merged to the branch mainline in revision 5225.
  • Revision ID: john@arbash-meinel.com-20100511141331-rizo2ez6bze3ao66
Some small tweaks to the chk_map code.

Find out that we actually weren't using the global definition because we
were assigning inside the if block. So factor that out into a helper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
    int PyString_CheckExact(object)
37
37
    char *PyString_AS_STRING(object s)
38
38
    Py_ssize_t PyString_GET_SIZE(object)
 
39
    unsigned long PyInt_AsUnsignedLongMask(object) except? -1
39
40
 
40
41
    int PyDict_SetItem(object d, object k, object v) except -1
41
42
 
54
55
# cimport all of the definitions we will need to access
55
56
from _static_tuple_c cimport StaticTuple,\
56
57
    import_static_tuple_c, StaticTuple_New, \
57
 
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
 
58
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact, \
 
59
    StaticTuple_GET_SIZE
58
60
 
59
61
cdef extern from "_static_tuple_c.h":
60
62
    # Defined explicitly rather than cimport-ing. Trying to use cimport, the
63
65
    PyObject * StaticTuple_GET_ITEM_ptr "StaticTuple_GET_ITEM" (StaticTuple,
64
66
                                                                Py_ssize_t)
65
67
 
66
 
cdef extern from "zlib.h":
67
 
    ctypedef unsigned long uLong
68
 
    ctypedef unsigned int uInt
69
 
    ctypedef unsigned char Bytef
70
 
 
71
 
    uLong crc32(uLong crc, Bytef *buf, uInt len)
 
68
cdef object crc32
 
69
from zlib import crc32
72
70
 
73
71
 
74
72
# Set up the StaticTuple C_API functionality
101
99
    cdef Py_ssize_t num_bits
102
100
    cdef Py_ssize_t i, j
103
101
    cdef Py_ssize_t num_out_bytes
104
 
    cdef Bytef *c_bit
105
 
    cdef uLong c_len
106
 
    cdef uInt crc_val
 
102
    cdef unsigned long crc_val
107
103
    cdef Py_ssize_t out_off
108
104
    cdef char *c_out
109
 
    cdef PyObject *bit
110
105
 
111
 
    if not StaticTuple_CheckExact(key):
112
 
        raise TypeError('key %r is not a StaticTuple' % (key,))
113
106
    num_bits = len(key)
114
107
    # 4 bytes per crc32, and another 1 byte between bits
115
108
    num_out_bytes = (9 * num_bits) - 1
119
112
        if i > 0:
120
113
            c_out[0] = c'\x00'
121
114
            c_out = c_out + 1
122
 
        # We use the _ptr variant, because GET_ITEM returns a borrowed
123
 
        # reference, and Pyrex assumes that returned 'object' are a new
124
 
        # reference
125
 
        bit = StaticTuple_GET_ITEM_ptr(key, i)
126
 
        if not PyString_CheckExact_ptr(bit):
127
 
            raise TypeError('Bit %d of %r is not a string' % (i, key))
128
 
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
129
 
        c_len = PyString_GET_SIZE_ptr(bit)
130
 
        crc_val = crc32(0, c_bit, c_len)
 
115
        crc_val = PyInt_AsUnsignedLongMask(crc32(key[i]))
131
116
        # Hex(val) order
132
117
        sprintf(c_out, '%08X', crc_val)
133
118
        c_out = c_out + 8
139
124
    cdef Py_ssize_t num_bits
140
125
    cdef Py_ssize_t i, j
141
126
    cdef Py_ssize_t num_out_bytes
142
 
    cdef Bytef *c_bit
143
 
    cdef uLong c_len
144
 
    cdef uInt crc_val
 
127
    cdef unsigned long crc_val
145
128
    cdef Py_ssize_t out_off
146
129
    cdef char *c_out
147
 
    cdef PyObject *bit
148
130
 
149
 
    if not StaticTuple_CheckExact(key):
150
 
        raise TypeError('key %r is not a StaticTuple' % (key,))
151
131
    num_bits = len(key)
152
132
    # 4 bytes per crc32, and another 1 byte between bits
153
133
    num_out_bytes = (5 * num_bits) - 1
157
137
        if i > 0:
158
138
            c_out[0] = c'\x00'
159
139
            c_out = c_out + 1
160
 
        bit = StaticTuple_GET_ITEM_ptr(key, i)
161
 
        if not PyString_CheckExact_ptr(bit):
162
 
            raise TypeError('Bit %d of %r is not a string: %r'
163
 
                            % (i, key, <object>bit))
164
 
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
165
 
        c_len = PyString_GET_SIZE_ptr(bit)
166
 
        crc_val = crc32(0, c_bit, c_len)
 
140
        crc_val = PyInt_AsUnsignedLongMask(crc32(key[i]))
167
141
        # MSB order
168
142
        c_out[0] = (crc_val >> 24) & 0xFF
169
143
        c_out[1] = (crc_val >> 16) & 0xFF
198
172
    return value
199
173
 
200
174
 
 
175
cdef _import_globals():
 
176
    """Set the global attributes. Done lazy to avoid recursive import loops."""
 
177
    global _LeafNode, _InternalNode, _unknown
 
178
 
 
179
    from bzrlib import chk_map
 
180
    _LeafNode = chk_map.LeafNode
 
181
    _InternalNode = chk_map.InternalNode
 
182
    _unknown = chk_map._unknown
 
183
 
 
184
 
201
185
def _deserialise_leaf_node(bytes, key, search_key_func=None):
202
186
    """Deserialise bytes, with key key, into a LeafNode.
203
187
 
215
199
    cdef StaticTuple entry_bits
216
200
 
217
201
    if _LeafNode is None:
218
 
        from bzrlib import chk_map
219
 
        _LeafNode = chk_map.LeafNode
220
 
        _InternalNode = chk_map.InternalNode
221
 
        _unknown = chk_map._unknown
 
202
        _import_globals()
222
203
 
223
204
    result = _LeafNode(search_key_func=search_key_func)
224
205
    # Splitlines can split on '\r' so don't use it, split('\n') adds an
322
303
                                               next_null - entry_start)
323
304
            Py_INCREF(entry)
324
305
            StaticTuple_SET_ITEM(entry_bits, i, entry)
325
 
        if len(entry_bits) != width:
 
306
        if StaticTuple_GET_SIZE(entry_bits) != width:
326
307
            raise AssertionError(
327
308
                'Incorrect number of elements (%d vs %d)'
328
309
                % (len(entry_bits)+1, width + 1))
358
339
    cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
359
340
 
360
341
    if _InternalNode is None:
361
 
        from bzrlib import chk_map
362
 
        _LeafNode = chk_map.LeafNode
363
 
        _InternalNode = chk_map.InternalNode
364
 
        _unknown = chk_map._unknown
 
342
        _import_globals()
365
343
    result = _InternalNode(search_key_func=search_key_func)
366
344
 
367
345
    if not StaticTuple_CheckExact(key):