~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_chk_map_pyx.pyx

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-05-07 08:01:38 UTC
  • mfrom: (5187.2.12 549310-mandatory-whoami)
  • Revision ID: pqm@pqm.ubuntu.com-20100507080138-doe642wej2tu47t5
(parthm) trivial docstring fix to indicate when errors.NoWhoami is raised.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    Py_ssize_t PyTuple_GET_SIZE(object t)
36
36
    int PyString_CheckExact(object)
37
37
    char *PyString_AS_STRING(object s)
38
 
    PyObject *PyString_FromStringAndSize_ptr "PyString_FromStringAndSize" (char *, Py_ssize_t)
39
38
    Py_ssize_t PyString_GET_SIZE(object)
40
 
    void PyString_InternInPlace(PyObject **)
41
 
    long PyInt_AS_LONG(object)
 
39
    unsigned long PyInt_AsUnsignedLongMask(object) except? -1
42
40
 
43
41
    int PyDict_SetItem(object d, object k, object v) except -1
44
42
 
 
43
    object PyTuple_New(Py_ssize_t count)
 
44
    void PyTuple_SET_ITEM(object t, Py_ssize_t offset, object)
 
45
 
45
46
    void Py_INCREF(object)
46
 
    void Py_DECREF_ptr "Py_DECREF" (PyObject *)
47
47
 
 
48
    PyObject * PyTuple_GET_ITEM_ptr "PyTuple_GET_ITEM" (object t,
 
49
                                                        Py_ssize_t offset)
 
50
    int PyString_CheckExact_ptr "PyString_CheckExact" (PyObject *p)
 
51
    Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *s)
 
52
    char *PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *s)
48
53
    object PyString_FromStringAndSize(char*, Py_ssize_t)
49
54
 
50
55
# cimport all of the definitions we will need to access
51
56
from _static_tuple_c cimport StaticTuple,\
52
57
    import_static_tuple_c, StaticTuple_New, \
53
 
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact, \
54
 
    StaticTuple_GET_SIZE
 
58
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
 
59
 
 
60
cdef extern from "_static_tuple_c.h":
 
61
    # Defined explicitly rather than cimport-ing. Trying to use cimport, the
 
62
    # type for PyObject is a different class that happens to have the same
 
63
    # name...
 
64
    PyObject * StaticTuple_GET_ITEM_ptr "StaticTuple_GET_ITEM" (StaticTuple,
 
65
                                                                Py_ssize_t)
55
66
 
56
67
cdef object crc32
57
68
from zlib import crc32
82
93
    return NULL
83
94
 
84
95
 
85
 
cdef object safe_interned_string_from_size(char *s, Py_ssize_t size):
86
 
    cdef PyObject *py_str
87
 
    if size < 0:
88
 
        raise AssertionError(
89
 
            'tried to create a string with an invalid size: %d @0x%x'
90
 
            % (size, <int>s))
91
 
    py_str = PyString_FromStringAndSize_ptr(s, size)
92
 
    PyString_InternInPlace(&py_str)
93
 
    result = <object>py_str
94
 
    # Casting a PyObject* to an <object> triggers an INCREF from Pyrex, so we
95
 
    # DECREF it to avoid geting immortal strings
96
 
    Py_DECREF_ptr(py_str)
97
 
    return result
98
 
 
99
 
 
100
96
def _search_key_16(key):
101
97
    """See chk_map._search_key_16."""
102
98
    cdef Py_ssize_t num_bits
115
111
        if i > 0:
116
112
            c_out[0] = c'\x00'
117
113
            c_out = c_out + 1
118
 
        crc_val = PyInt_AS_LONG(crc32(key[i]))
 
114
        crc_val = PyInt_AsUnsignedLongMask(crc32(key[i]))
119
115
        # Hex(val) order
120
116
        sprintf(c_out, '%08X', crc_val)
121
117
        c_out = c_out + 8
140
136
        if i > 0:
141
137
            c_out[0] = c'\x00'
142
138
            c_out = c_out + 1
143
 
        crc_val = PyInt_AS_LONG(crc32(key[i]))
 
139
        crc_val = PyInt_AsUnsignedLongMask(crc32(key[i]))
144
140
        # MSB order
145
141
        c_out[0] = (crc_val >> 24) & 0xFF
146
142
        c_out[1] = (crc_val >> 16) & 0xFF
175
171
    return value
176
172
 
177
173
 
178
 
cdef _import_globals():
179
 
    """Set the global attributes. Done lazy to avoid recursive import loops."""
180
 
    global _LeafNode, _InternalNode, _unknown
181
 
 
182
 
    from bzrlib import chk_map
183
 
    _LeafNode = chk_map.LeafNode
184
 
    _InternalNode = chk_map.InternalNode
185
 
    _unknown = chk_map._unknown
186
 
 
187
 
 
188
174
def _deserialise_leaf_node(bytes, key, search_key_func=None):
189
175
    """Deserialise bytes, with key key, into a LeafNode.
190
176
 
202
188
    cdef StaticTuple entry_bits
203
189
 
204
190
    if _LeafNode is None:
205
 
        _import_globals()
 
191
        from bzrlib import chk_map
 
192
        _LeafNode = chk_map.LeafNode
 
193
        _InternalNode = chk_map.InternalNode
 
194
        _unknown = chk_map._unknown
206
195
 
207
196
    result = _LeafNode(search_key_func=search_key_func)
208
197
    # Splitlines can split on '\r' so don't use it, split('\n') adds an
306
295
                                               next_null - entry_start)
307
296
            Py_INCREF(entry)
308
297
            StaticTuple_SET_ITEM(entry_bits, i, entry)
309
 
        if StaticTuple_GET_SIZE(entry_bits) != width:
 
298
        if len(entry_bits) != width:
310
299
            raise AssertionError(
311
300
                'Incorrect number of elements (%d vs %d)'
312
301
                % (len(entry_bits)+1, width + 1))
342
331
    cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
343
332
 
344
333
    if _InternalNode is None:
345
 
        _import_globals()
 
334
        from bzrlib import chk_map
 
335
        _LeafNode = chk_map.LeafNode
 
336
        _InternalNode = chk_map.InternalNode
 
337
        _unknown = chk_map._unknown
346
338
    result = _InternalNode(search_key_func=search_key_func)
347
339
 
348
340
    if not StaticTuple_CheckExact(key):
403
395
    result._node_width = len(item_prefix)
404
396
    result._search_prefix = PyString_FromStringAndSize(prefix, prefix_length)
405
397
    return result
406
 
 
407
 
 
408
 
def _bytes_to_text_key(bytes):
409
 
    """Take a CHKInventory value string and return a (file_id, rev_id) tuple"""
410
 
    cdef StaticTuple key
411
 
    cdef char *byte_str, *cur_end, *file_id_str, *byte_end
412
 
    cdef char *revision_str
413
 
    cdef Py_ssize_t byte_size, pos, file_id_len
414
 
 
415
 
    if not PyString_CheckExact(bytes):
416
 
        raise TypeError('bytes must be a string, got %r' % (type(bytes),))
417
 
    byte_str = PyString_AS_STRING(bytes)
418
 
    byte_size = PyString_GET_SIZE(bytes)
419
 
    byte_end = byte_str + byte_size
420
 
    cur_end = <char*>memchr(byte_str, c':', byte_size)
421
 
    if cur_end == NULL:
422
 
        raise ValueError('No kind section found.')
423
 
    if cur_end[1] != c' ':
424
 
        raise ValueError(
425
 
            'Kind section should end with ": ", got %r' % str(cur_end[:2],))
426
 
    file_id_str = cur_end + 2
427
 
    # file_id is now the data up until the next newline
428
 
    cur_end = <char*>memchr(file_id_str, c'\n', byte_end - file_id_str)
429
 
    if cur_end == NULL:
430
 
        raise ValueError('no newline after file-id')
431
 
    file_id = safe_interned_string_from_size(file_id_str,
432
 
                                             cur_end - file_id_str)
433
 
    # this is the end of the parent_str
434
 
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
435
 
    if cur_end == NULL:
436
 
        raise ValueError('no newline after parent_str')
437
 
    # end of the name str
438
 
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
439
 
    if cur_end == NULL:
440
 
        raise ValueError('no newline after name str')
441
 
    # the next section is the revision info
442
 
    revision_str = cur_end + 1
443
 
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
444
 
    if cur_end == NULL:
445
 
        # This is probably a dir: entry, which has revision as the last item
446
 
        cur_end = byte_end
447
 
    revision = safe_interned_string_from_size(revision_str,
448
 
        cur_end - revision_str)
449
 
    key = StaticTuple_New(2)
450
 
    Py_INCREF(file_id)
451
 
    StaticTuple_SET_ITEM(key, 0, file_id) 
452
 
    Py_INCREF(revision)
453
 
    StaticTuple_SET_ITEM(key, 1, revision) 
454
 
    return StaticTuple_Intern(key)