/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/_chk_map_pyx.pyx

  • Committer: Martin
  • Date: 2017-06-11 14:36:07 UTC
  • mto: This revision was merged to the branch mainline in revision 6688.
  • Revision ID: gzlist@googlemail.com-20170611143607-iuusrtbtuvgzrcho
Make test__rio pass on Python 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
 
18
 
#python2.4 support
 
17
from __future__ import absolute_import
 
18
 
 
19
 
 
20
from __future__ import absolute_import
 
21
 
19
22
cdef extern from "python-compat.h":
20
23
    pass
21
24
 
35
38
    Py_ssize_t PyTuple_GET_SIZE(object t)
36
39
    int PyString_CheckExact(object)
37
40
    char *PyString_AS_STRING(object s)
 
41
    PyObject *PyString_FromStringAndSize_ptr "PyString_FromStringAndSize" (char *, Py_ssize_t)
38
42
    Py_ssize_t PyString_GET_SIZE(object)
 
43
    void PyString_InternInPlace(PyObject **)
 
44
    long PyInt_AS_LONG(object)
39
45
 
40
46
    int PyDict_SetItem(object d, object k, object v) except -1
41
47
 
42
 
    object PyTuple_New(Py_ssize_t count)
43
 
    void PyTuple_SET_ITEM(object t, Py_ssize_t offset, object)
44
 
 
45
48
    void Py_INCREF(object)
 
49
    void Py_DECREF_ptr "Py_DECREF" (PyObject *)
46
50
 
47
 
    PyObject * PyTuple_GET_ITEM_ptr "PyTuple_GET_ITEM" (object t,
48
 
                                                        Py_ssize_t offset)
49
 
    int PyString_CheckExact_ptr "PyString_CheckExact" (PyObject *p)
50
 
    Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *s)
51
 
    char *PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *s)
52
51
    object PyString_FromStringAndSize(char*, Py_ssize_t)
53
52
 
54
53
# cimport all of the definitions we will need to access
55
 
from _static_tuple_c cimport StaticTuple,\
 
54
from ._static_tuple_c cimport StaticTuple,\
56
55
    import_static_tuple_c, StaticTuple_New, \
57
 
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
58
 
 
59
 
cdef extern from "_static_tuple_c.h":
60
 
    # Defined explicitly rather than cimport-ing. Trying to use cimport, the
61
 
    # type for PyObject is a different class that happens to have the same
62
 
    # name...
63
 
    PyObject * StaticTuple_GET_ITEM_ptr "StaticTuple_GET_ITEM" (StaticTuple,
64
 
                                                                Py_ssize_t)
65
 
 
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)
 
56
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact, \
 
57
    StaticTuple_GET_SIZE
 
58
 
 
59
cdef object crc32
 
60
from zlib import crc32
72
61
 
73
62
 
74
63
# Set up the StaticTuple C_API functionality
96
85
    return NULL
97
86
 
98
87
 
 
88
cdef object safe_interned_string_from_size(char *s, Py_ssize_t size):
 
89
    cdef PyObject *py_str
 
90
    if size < 0:
 
91
        raise AssertionError(
 
92
            'tried to create a string with an invalid size: %d @0x%x'
 
93
            % (size, <int>s))
 
94
    py_str = PyString_FromStringAndSize_ptr(s, size)
 
95
    PyString_InternInPlace(&py_str)
 
96
    result = <object>py_str
 
97
    # Casting a PyObject* to an <object> triggers an INCREF from Pyrex, so we
 
98
    # DECREF it to avoid geting immortal strings
 
99
    Py_DECREF_ptr(py_str)
 
100
    return result
 
101
 
 
102
 
99
103
def _search_key_16(key):
100
104
    """See chk_map._search_key_16."""
101
105
    cdef Py_ssize_t num_bits
102
106
    cdef Py_ssize_t i, j
103
107
    cdef Py_ssize_t num_out_bytes
104
 
    cdef Bytef *c_bit
105
 
    cdef uLong c_len
106
 
    cdef uInt crc_val
 
108
    cdef unsigned long crc_val
107
109
    cdef Py_ssize_t out_off
108
110
    cdef char *c_out
109
 
    cdef PyObject *bit
110
111
 
111
 
    if not StaticTuple_CheckExact(key):
112
 
        raise TypeError('key %r is not a StaticTuple' % (key,))
113
112
    num_bits = len(key)
114
113
    # 4 bytes per crc32, and another 1 byte between bits
115
114
    num_out_bytes = (9 * num_bits) - 1
119
118
        if i > 0:
120
119
            c_out[0] = c'\x00'
121
120
            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)
 
121
        crc_val = PyInt_AS_LONG(crc32(key[i]))
131
122
        # Hex(val) order
132
123
        sprintf(c_out, '%08X', crc_val)
133
124
        c_out = c_out + 8
139
130
    cdef Py_ssize_t num_bits
140
131
    cdef Py_ssize_t i, j
141
132
    cdef Py_ssize_t num_out_bytes
142
 
    cdef Bytef *c_bit
143
 
    cdef uLong c_len
144
 
    cdef uInt crc_val
 
133
    cdef unsigned long crc_val
145
134
    cdef Py_ssize_t out_off
146
135
    cdef char *c_out
147
 
    cdef PyObject *bit
148
136
 
149
 
    if not StaticTuple_CheckExact(key):
150
 
        raise TypeError('key %r is not a StaticTuple' % (key,))
151
137
    num_bits = len(key)
152
138
    # 4 bytes per crc32, and another 1 byte between bits
153
139
    num_out_bytes = (5 * num_bits) - 1
157
143
        if i > 0:
158
144
            c_out[0] = c'\x00'
159
145
            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)
 
146
        crc_val = PyInt_AS_LONG(crc32(key[i]))
167
147
        # MSB order
168
148
        c_out[0] = (crc_val >> 24) & 0xFF
169
149
        c_out[1] = (crc_val >> 16) & 0xFF
198
178
    return value
199
179
 
200
180
 
 
181
cdef _import_globals():
 
182
    """Set the global attributes. Done lazy to avoid recursive import loops."""
 
183
    global _LeafNode, _InternalNode, _unknown
 
184
 
 
185
    from . import chk_map
 
186
    _LeafNode = chk_map.LeafNode
 
187
    _InternalNode = chk_map.InternalNode
 
188
    _unknown = chk_map._unknown
 
189
 
 
190
 
201
191
def _deserialise_leaf_node(bytes, key, search_key_func=None):
202
192
    """Deserialise bytes, with key key, into a LeafNode.
203
193
 
215
205
    cdef StaticTuple entry_bits
216
206
 
217
207
    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
 
208
        _import_globals()
222
209
 
223
210
    result = _LeafNode(search_key_func=search_key_func)
224
211
    # Splitlines can split on '\r' so don't use it, split('\n') adds an
322
309
                                               next_null - entry_start)
323
310
            Py_INCREF(entry)
324
311
            StaticTuple_SET_ITEM(entry_bits, i, entry)
325
 
        if len(entry_bits) != width:
 
312
        if StaticTuple_GET_SIZE(entry_bits) != width:
326
313
            raise AssertionError(
327
314
                'Incorrect number of elements (%d vs %d)'
328
315
                % (len(entry_bits)+1, width + 1))
358
345
    cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
359
346
 
360
347
    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
 
348
        _import_globals()
365
349
    result = _InternalNode(search_key_func=search_key_func)
366
350
 
367
351
    if not StaticTuple_CheckExact(key):
422
406
    result._node_width = len(item_prefix)
423
407
    result._search_prefix = PyString_FromStringAndSize(prefix, prefix_length)
424
408
    return result
 
409
 
 
410
 
 
411
def _bytes_to_text_key(bytes):
 
412
    """Take a CHKInventory value string and return a (file_id, rev_id) tuple"""
 
413
    cdef StaticTuple key
 
414
    cdef char *byte_str, *cur_end, *file_id_str, *byte_end
 
415
    cdef char *revision_str
 
416
    cdef Py_ssize_t byte_size, pos, file_id_len
 
417
 
 
418
    if not PyString_CheckExact(bytes):
 
419
        raise TypeError('bytes must be a string, got %r' % (type(bytes),))
 
420
    byte_str = PyString_AS_STRING(bytes)
 
421
    byte_size = PyString_GET_SIZE(bytes)
 
422
    byte_end = byte_str + byte_size
 
423
    cur_end = <char*>memchr(byte_str, c':', byte_size)
 
424
    if cur_end == NULL:
 
425
        raise ValueError('No kind section found.')
 
426
    if cur_end[1] != c' ':
 
427
        raise ValueError(
 
428
            'Kind section should end with ": ", got %r' % str(cur_end[:2],))
 
429
    file_id_str = cur_end + 2
 
430
    # file_id is now the data up until the next newline
 
431
    cur_end = <char*>memchr(file_id_str, c'\n', byte_end - file_id_str)
 
432
    if cur_end == NULL:
 
433
        raise ValueError('no newline after file-id')
 
434
    file_id = safe_interned_string_from_size(file_id_str,
 
435
                                             cur_end - file_id_str)
 
436
    # this is the end of the parent_str
 
437
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
 
438
    if cur_end == NULL:
 
439
        raise ValueError('no newline after parent_str')
 
440
    # end of the name str
 
441
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
 
442
    if cur_end == NULL:
 
443
        raise ValueError('no newline after name str')
 
444
    # the next section is the revision info
 
445
    revision_str = cur_end + 1
 
446
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
 
447
    if cur_end == NULL:
 
448
        # This is probably a dir: entry, which has revision as the last item
 
449
        cur_end = byte_end
 
450
    revision = safe_interned_string_from_size(revision_str,
 
451
        cur_end - revision_str)
 
452
    key = StaticTuple_New(2)
 
453
    Py_INCREF(file_id)
 
454
    StaticTuple_SET_ITEM(key, 0, file_id) 
 
455
    Py_INCREF(revision)
 
456
    StaticTuple_SET_ITEM(key, 1, revision) 
 
457
    return StaticTuple_Intern(key)