30
30
cdef extern from "Python.h":
31
31
ctypedef int Py_ssize_t # Required for older pyrex versions
32
ctypedef struct PyObject:
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)
38
37
char *PyString_AS_STRING(object s)
38
PyObject *PyString_FromStringAndSize_ptr "PyString_FromStringAndSize" (char *, Py_ssize_t)
39
39
Py_ssize_t PyString_GET_SIZE(object)
40
void PyString_InternInPlace(PyObject **)
41
long PyInt_AS_LONG(object)
41
43
int PyDict_SetItem(object d, object k, object v) except -1
43
object PyTuple_New(Py_ssize_t count)
44
void PyTuple_SET_ITEM(object t, Py_ssize_t offset, object)
46
45
void Py_INCREF(object)
46
void Py_DECREF_ptr "Py_DECREF" (PyObject *)
48
PyObject * PyTuple_GET_ITEM_ptr "PyTuple_GET_ITEM" (object t,
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)
53
48
object PyString_FromStringAndSize(char*, Py_ssize_t)
55
cdef extern from "zlib.h":
56
ctypedef unsigned long uLong
57
ctypedef unsigned int uInt
58
ctypedef unsigned char Bytef
60
uLong crc32(uLong crc, Bytef *buf, uInt len)
50
# cimport all of the definitions we will need to access
51
from _static_tuple_c cimport StaticTuple,\
52
import_static_tuple_c, StaticTuple_New, \
53
StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact, \
57
from zlib import crc32
60
# Set up the StaticTuple C_API functionality
61
import_static_tuple_c()
65
cdef object _InternalNode
64
66
_InternalNode = None
67
70
# We shouldn't just copy this from _dirstate_helpers_pyx
68
cdef void* _my_memrchr(void *s, int c, size_t n):
71
cdef void* _my_memrchr(void *s, int c, size_t n): # cannot_raise
69
72
# memrchr seems to be a GNU extension, so we have to implement it ourselves
85
cdef object safe_interned_string_from_size(char *s, Py_ssize_t size):
89
'tried to create a string with an invalid size: %d @0x%x'
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
82
100
def _search_key_16(key):
83
101
"""See chk_map._search_key_16."""
84
102
cdef Py_ssize_t num_bits
85
103
cdef Py_ssize_t i, j
86
104
cdef Py_ssize_t num_out_bytes
105
cdef unsigned long crc_val
90
106
cdef Py_ssize_t out_off
94
if not PyTuple_CheckExact(key):
95
raise TypeError('key %r is not a tuple' % (key,))
96
num_bits = PyTuple_GET_SIZE(key)
97
110
# 4 bytes per crc32, and another 1 byte between bits
98
111
num_out_bytes = (9 * num_bits) - 1
99
112
out = PyString_FromStringAndSize(NULL, num_out_bytes)
103
116
c_out[0] = c'\x00'
104
117
c_out = c_out + 1
105
# We use the _ptr variant, because GET_ITEM returns a borrowed
106
# reference, and Pyrex assumes that returned 'object' are a new
108
bit = PyTuple_GET_ITEM_ptr(key, i)
109
if not PyString_CheckExact_ptr(bit):
110
raise TypeError('Bit %d of %r is not a string' % (i, key))
111
c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
112
c_len = PyString_GET_SIZE_ptr(bit)
113
crc_val = crc32(0, c_bit, c_len)
118
crc_val = PyInt_AS_LONG(crc32(key[i]))
115
120
sprintf(c_out, '%08X', crc_val)
116
121
c_out = c_out + 8
122
127
cdef Py_ssize_t num_bits
123
128
cdef Py_ssize_t i, j
124
129
cdef Py_ssize_t num_out_bytes
130
cdef unsigned long crc_val
128
131
cdef Py_ssize_t out_off
132
if not PyTuple_CheckExact(key):
133
raise TypeError('key %r is not a tuple' % (key,))
134
num_bits = PyTuple_GET_SIZE(key)
135
135
# 4 bytes per crc32, and another 1 byte between bits
136
136
num_out_bytes = (5 * num_bits) - 1
137
137
out = PyString_FromStringAndSize(NULL, num_out_bytes)
141
141
c_out[0] = c'\x00'
142
142
c_out = c_out + 1
143
bit = PyTuple_GET_ITEM_ptr(key, i)
144
if not PyString_CheckExact_ptr(bit):
145
raise TypeError('Bit %d of %r is not a string: %r' % (i, key,
147
c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
148
c_len = PyString_GET_SIZE_ptr(bit)
149
crc_val = crc32(0, c_bit, c_len)
143
crc_val = PyInt_AS_LONG(crc32(key[i]))
151
145
c_out[0] = (crc_val >> 24) & 0xFF
152
146
c_out[1] = (crc_val >> 16) & 0xFF
178
cdef _import_globals():
179
"""Set the global attributes. Done lazy to avoid recursive import loops."""
180
global _LeafNode, _InternalNode, _unknown
182
from bzrlib import chk_map
183
_LeafNode = chk_map.LeafNode
184
_InternalNode = chk_map.InternalNode
185
_unknown = chk_map._unknown
184
188
def _deserialise_leaf_node(bytes, key, search_key_func=None):
185
189
"""Deserialise bytes, with key key, into a LeafNode.
195
199
cdef char *prefix, *value_start, *prefix_tail
196
200
cdef char *next_null, *last_null, *line_start
197
201
cdef char *c_entry, *entry_start
202
cdef StaticTuple entry_bits
199
204
if _LeafNode is None:
200
from bzrlib import chk_map
201
_LeafNode = chk_map.LeafNode
202
_InternalNode = chk_map.InternalNode
203
_unknown = chk_map._unknown
205
207
result = _LeafNode(search_key_func=search_key_func)
206
208
# Splitlines can split on '\r' so don't use it, split('\n') adds an
265
267
if next_line == NULL:
266
268
raise ValueError('missing trailing newline')
267
269
cur = next_line + 1
268
entry_bits = PyTuple_New(width)
270
entry_bits = StaticTuple_New(width)
269
271
for i from 0 <= i < num_prefix_bits:
272
# TODO: Use PyList_GetItem, or turn prefix_bits into a
270
274
entry = prefix_bits[i]
271
275
# SET_ITEM 'steals' a reference
273
PyTuple_SET_ITEM(entry_bits, i, entry)
277
StaticTuple_SET_ITEM(entry_bits, i, entry)
274
278
value = PyString_FromStringAndSize(value_start, next_line - value_start)
275
279
# The next entry bit needs the 'tail' from the prefix, and first part
288
292
memcpy(c_entry + prefix_tail_len, line_start, next_null - line_start)
290
294
i = num_prefix_bits
291
PyTuple_SET_ITEM(entry_bits, i, entry)
295
StaticTuple_SET_ITEM(entry_bits, i, entry)
292
296
while next_null != last_null: # We have remaining bits
301
305
entry = PyString_FromStringAndSize(entry_start,
302
306
next_null - entry_start)
304
PyTuple_SET_ITEM(entry_bits, i, entry)
305
if len(entry_bits) != width:
308
StaticTuple_SET_ITEM(entry_bits, i, entry)
309
if StaticTuple_GET_SIZE(entry_bits) != width:
306
310
raise AssertionError(
307
311
'Incorrect number of elements (%d vs %d)'
308
312
% (len(entry_bits)+1, width + 1))
313
entry_bits = StaticTuple_Intern(entry_bits)
309
314
PyDict_SetItem(items, entry_bits, value)
310
315
if len(items) != length:
311
316
raise ValueError("item count (%d) mismatch for key %s,"
337
342
cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
339
344
if _InternalNode is None:
340
from bzrlib import chk_map
341
_LeafNode = chk_map.LeafNode
342
_InternalNode = chk_map.InternalNode
343
_unknown = chk_map._unknown
344
346
result = _InternalNode(search_key_func=search_key_func)
348
if not StaticTuple_CheckExact(key):
349
raise TypeError('key %r is not a StaticTuple' % (key,))
346
350
if not PyString_CheckExact(bytes):
347
351
raise TypeError('bytes must be a plain string not %s' % (type(bytes),))
384
388
memcpy(c_item_prefix + prefix_length, cur, next_null - cur)
385
389
flat_key = PyString_FromStringAndSize(next_null + 1,
386
390
next_line - next_null - 1)
387
PyDict_SetItem(items, item_prefix, (flat_key,))
391
flat_key = StaticTuple(flat_key).intern()
392
PyDict_SetItem(items, item_prefix, flat_key)
388
393
cur = next_line + 1
389
394
assert len(items) > 0
390
395
result._items = items
399
404
result._search_prefix = PyString_FromStringAndSize(prefix, prefix_length)
408
def _bytes_to_text_key(bytes):
409
"""Take a CHKInventory value string and return a (file_id, rev_id) tuple"""
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
415
if not PyString_CheckExact(bytes):
416
raise TypeError('bytes must be a string')
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)
422
raise ValueError('No kind section found.')
423
if cur_end[1] != c' ':
424
raise ValueError('Kind section should end with ": "')
425
file_id_str = cur_end + 2
426
# file_id is now the data up until the next newline
427
cur_end = <char*>memchr(file_id_str, c'\n', byte_end - file_id_str)
429
raise ValueError('no newline after file-id')
430
file_id = safe_interned_string_from_size(file_id_str,
431
cur_end - file_id_str)
432
# this is the end of the parent_str
433
cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
435
raise ValueError('no newline after parent_str')
436
# end of the name str
437
cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
439
raise ValueError('no newline after name str')
440
# the next section is the revision info
441
revision_str = cur_end + 1
442
cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
444
# This is probably a dir: entry, which has revision as the last item
446
revision = safe_interned_string_from_size(revision_str,
447
cur_end - revision_str)
448
key = StaticTuple_New(2)
450
StaticTuple_SET_ITEM(key, 0, file_id)
452
StaticTuple_SET_ITEM(key, 1, revision)
453
return StaticTuple_Intern(key)