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)
38
39
Py_ssize_t PyString_GET_SIZE(object)
40
void PyString_InternInPlace(PyObject **)
39
41
unsigned long PyInt_AsUnsignedLongMask(object) except? -1
41
43
int PyDict_SetItem(object d, object k, object v) except -1
55
58
# cimport all of the definitions we will need to access
56
59
from _static_tuple_c cimport StaticTuple,\
57
60
import_static_tuple_c, StaticTuple_New, \
58
StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
61
StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact, \
60
64
cdef extern from "_static_tuple_c.h":
61
65
# Defined explicitly rather than cimport-ing. Trying to use cimport, the
100
cdef object safe_interned_string_from_size(char *s, Py_ssize_t size):
101
cdef PyObject *py_str
103
raise AssertionError(
104
'tried to create a string with an invalid size: %d @0x%x'
106
py_str = PyString_FromStringAndSize_ptr(s, size)
107
PyString_InternInPlace(&py_str)
108
result = <object>py_str
109
# Casting a PyObject* to an <object> triggers an INCREF from Pyrex, so we
110
# DECREF it to avoid geting immortal strings
111
Py_DECREF_ptr(py_str)
96
115
def _search_key_16(key):
97
116
"""See chk_map._search_key_16."""
98
117
cdef Py_ssize_t num_bits
193
cdef _import_globals():
194
"""Set the global attributes. Done lazy to avoid recursive import loops."""
195
global _LeafNode, _InternalNode, _unknown
197
from bzrlib import chk_map
198
_LeafNode = chk_map.LeafNode
199
_InternalNode = chk_map.InternalNode
200
_unknown = chk_map._unknown
174
203
def _deserialise_leaf_node(bytes, key, search_key_func=None):
175
204
"""Deserialise bytes, with key key, into a LeafNode.
188
217
cdef StaticTuple entry_bits
190
219
if _LeafNode is None:
191
from bzrlib import chk_map
192
_LeafNode = chk_map.LeafNode
193
_InternalNode = chk_map.InternalNode
194
_unknown = chk_map._unknown
196
222
result = _LeafNode(search_key_func=search_key_func)
197
223
# Splitlines can split on '\r' so don't use it, split('\n') adds an
295
321
next_null - entry_start)
297
323
StaticTuple_SET_ITEM(entry_bits, i, entry)
298
if len(entry_bits) != width:
324
if StaticTuple_GET_SIZE(entry_bits) != width:
299
325
raise AssertionError(
300
326
'Incorrect number of elements (%d vs %d)'
301
327
% (len(entry_bits)+1, width + 1))
331
357
cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
333
359
if _InternalNode is None:
334
from bzrlib import chk_map
335
_LeafNode = chk_map.LeafNode
336
_InternalNode = chk_map.InternalNode
337
_unknown = chk_map._unknown
338
361
result = _InternalNode(search_key_func=search_key_func)
340
363
if not StaticTuple_CheckExact(key):
395
418
result._node_width = len(item_prefix)
396
419
result._search_prefix = PyString_FromStringAndSize(prefix, prefix_length)
423
def _bytes_to_text_key(bytes):
424
"""Take a CHKInventory value string and return a (file_id, rev_id) tuple"""
426
cdef char *byte_str, *cur_end, *file_id_str, *byte_end
427
cdef char *revision_str
428
cdef Py_ssize_t byte_size, pos, file_id_len
430
if not PyString_CheckExact(bytes):
431
raise TypeError('bytes must be a string')
432
byte_str = PyString_AS_STRING(bytes)
433
byte_size = PyString_GET_SIZE(bytes)
434
byte_end = byte_str + byte_size
435
cur_end = <char*>memchr(byte_str, c':', byte_size)
437
raise ValueError('No kind section found.')
438
if cur_end[1] != c' ':
439
raise ValueError('Kind section should end with ": "')
440
file_id_str = cur_end + 2
441
# file_id is now the data up until the next newline
442
cur_end = <char*>memchr(file_id_str, c'\n', byte_end - file_id_str)
444
raise ValueError('no newline after file-id')
445
file_id = safe_interned_string_from_size(file_id_str,
446
cur_end - file_id_str)
447
# this is the end of the parent_str
448
cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
450
raise ValueError('no newline after parent_str')
451
# end of the name str
452
cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
454
raise ValueError('no newline after name str')
455
# the next section is the revision info
456
revision_str = cur_end + 1
457
cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
459
# This is probably a dir: entry, which has revision as the last item
461
revision = safe_interned_string_from_size(revision_str,
462
cur_end - revision_str)
463
key = StaticTuple_New(2)
465
StaticTuple_SET_ITEM(key, 0, file_id)
467
StaticTuple_SET_ITEM(key, 1, revision)
468
return StaticTuple_Intern(key)