~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_btree_serializer_pyx.pyx

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-08-27 02:27:19 UTC
  • mfrom: (4634.3.19 gc-batching)
  • Revision ID: pqm@pqm.ubuntu.com-20090827022719-bl2yoqhpj3fcfczu
(andrew) Fix #402657: 2a fetch over dumb transport reads one group at
        a time.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
#
17
17
 
18
18
"""Pyrex extensions to btree node parsing."""
19
19
 
 
20
#python2.4 support
 
21
cdef extern from "python-compat.h":
 
22
    pass
 
23
 
20
24
cdef extern from "stdlib.h":
21
25
    ctypedef unsigned size_t
22
26
 
28
32
 
29
33
    char *PyString_AsString(object p) except NULL
30
34
    object PyString_FromStringAndSize(char *, Py_ssize_t)
 
35
    PyObject *PyString_FromStringAndSize_ptr "PyString_FromStringAndSize" (char *, Py_ssize_t)
31
36
    int PyString_CheckExact(object s)
32
37
    int PyString_CheckExact_ptr "PyString_CheckExact" (PyObject *)
33
38
    Py_ssize_t PyString_Size(object p)
34
39
    Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *)
35
40
    char * PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *)
36
41
    int PyString_AsStringAndSize_ptr(PyObject *, char **buf, Py_ssize_t *len)
 
42
    void PyString_InternInPlace(PyObject **)
37
43
    int PyTuple_CheckExact(object t)
38
44
    Py_ssize_t PyTuple_GET_SIZE(object t)
39
45
    PyObject *PyTuple_GET_ITEM_ptr_object "PyTuple_GET_ITEM" (object tpl, int index)
 
46
    void Py_DECREF_ptr "Py_DECREF" (PyObject *)
40
47
 
41
48
cdef extern from "string.h":
42
49
    void *memcpy(void *dest, void *src, size_t n)
70
77
    return PyString_FromStringAndSize(s, size)
71
78
 
72
79
 
 
80
cdef object safe_interned_string_from_size(char *s, Py_ssize_t size):
 
81
    cdef PyObject *py_str
 
82
    if size < 0:
 
83
        raise AssertionError(
 
84
            'tried to create a string with an invalid size: %d @0x%x'
 
85
            % (size, <int>s))
 
86
    py_str = PyString_FromStringAndSize_ptr(s, size)
 
87
    PyString_InternInPlace(&py_str)
 
88
    result = <object>py_str
 
89
    # Casting a PyObject* to an <object> triggers an INCREF from Pyrex, so we
 
90
    # DECREF it to avoid geting immortal strings
 
91
    Py_DECREF_ptr(py_str)
 
92
    return result
 
93
 
 
94
 
73
95
cdef class BTreeLeafParser:
74
96
    """Parse the leaf nodes of a BTree index.
75
97
 
138
160
            # TODO: Consider using PyIntern_FromString, the only caveat is that
139
161
            # it assumes a NULL-terminated string, so we have to check if
140
162
            # temp_ptr[0] == c'\0' or some other char.
141
 
            key_element = safe_string_from_size(self._start,
142
 
                                                temp_ptr - self._start)
 
163
            key_element = safe_interned_string_from_size(self._start,
 
164
                                                         temp_ptr - self._start)
143
165
            # advance our pointer
144
166
            self._start = temp_ptr + 1
145
167
            PyList_Append(key_segments, key_element)