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)
43
40
int PyDict_SetItem(object d, object k, object v) except -1
42
object PyTuple_New(Py_ssize_t count)
43
void PyTuple_SET_ITEM(object t, Py_ssize_t offset, object)
45
45
void Py_INCREF(object)
46
void Py_DECREF_ptr "Py_DECREF" (PyObject *)
47
PyObject * PyTuple_GET_ITEM_ptr "PyTuple_GET_ITEM" (object t,
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)
48
52
object PyString_FromStringAndSize(char*, Py_ssize_t)
50
54
# cimport all of the definitions we will need to access
51
55
from _static_tuple_c cimport StaticTuple,\
52
56
import_static_tuple_c, StaticTuple_New, \
53
StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact, \
57
from zlib import crc32
57
StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
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
63
PyObject * StaticTuple_GET_ITEM_ptr "StaticTuple_GET_ITEM" (StaticTuple,
66
cdef extern from "zlib.h":
67
ctypedef unsigned long uLong
68
ctypedef unsigned int uInt
69
ctypedef unsigned char Bytef
71
uLong crc32(uLong crc, Bytef *buf, uInt len)
60
74
# Set up the StaticTuple C_API functionality
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
100
99
def _search_key_16(key):
101
100
"""See chk_map._search_key_16."""
102
101
cdef Py_ssize_t num_bits
103
102
cdef Py_ssize_t i, j
104
103
cdef Py_ssize_t num_out_bytes
105
cdef unsigned long crc_val
106
107
cdef Py_ssize_t out_off
111
if not StaticTuple_CheckExact(key):
112
raise TypeError('key %r is not a StaticTuple' % (key,))
109
113
num_bits = len(key)
110
114
# 4 bytes per crc32, and another 1 byte between bits
111
115
num_out_bytes = (9 * num_bits) - 1
116
120
c_out[0] = c'\x00'
117
121
c_out = c_out + 1
118
crc_val = PyInt_AS_LONG(crc32(key[i]))
122
# We use the _ptr variant, because GET_ITEM returns a borrowed
123
# reference, and Pyrex assumes that returned 'object' are a new
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)
120
132
sprintf(c_out, '%08X', crc_val)
121
133
c_out = c_out + 8
127
139
cdef Py_ssize_t num_bits
128
140
cdef Py_ssize_t i, j
129
141
cdef Py_ssize_t num_out_bytes
130
cdef unsigned long crc_val
131
145
cdef Py_ssize_t out_off
149
if not StaticTuple_CheckExact(key):
150
raise TypeError('key %r is not a StaticTuple' % (key,))
134
151
num_bits = len(key)
135
152
# 4 bytes per crc32, and another 1 byte between bits
136
153
num_out_bytes = (5 * num_bits) - 1
141
158
c_out[0] = c'\x00'
142
159
c_out = c_out + 1
143
crc_val = PyInt_AS_LONG(crc32(key[i]))
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)
145
168
c_out[0] = (crc_val >> 24) & 0xFF
146
169
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
188
201
def _deserialise_leaf_node(bytes, key, search_key_func=None):
189
202
"""Deserialise bytes, with key key, into a LeafNode.
202
215
cdef StaticTuple entry_bits
204
217
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
207
223
result = _LeafNode(search_key_func=search_key_func)
208
224
# Splitlines can split on '\r' so don't use it, split('\n') adds an
306
322
next_null - entry_start)
308
324
StaticTuple_SET_ITEM(entry_bits, i, entry)
309
if StaticTuple_GET_SIZE(entry_bits) != width:
325
if len(entry_bits) != width:
310
326
raise AssertionError(
311
327
'Incorrect number of elements (%d vs %d)'
312
328
% (len(entry_bits)+1, width + 1))
342
358
cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
344
360
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
346
365
result = _InternalNode(search_key_func=search_key_func)
348
367
if not StaticTuple_CheckExact(key):
403
422
result._node_width = len(item_prefix)
404
423
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)