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

Merge test-run support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from __future__ import absolute_import
20
20
 
21
 
from cpython.bool cimport (
22
 
    PyBool_Check,
23
 
    )
24
21
from cpython.bytes cimport (
25
22
    PyBytes_CheckExact,
26
23
    PyBytes_FromStringAndSize,
27
24
    PyBytes_AS_STRING,
28
25
    PyBytes_GET_SIZE,
29
26
    )
30
 
from cpython.dict cimport (
31
 
    PyDict_CheckExact,
 
27
from cpython.long cimport (
 
28
    PyLong_CheckExact,
32
29
    )
33
30
from cpython.int cimport (
34
31
    PyInt_CheckExact,
35
32
    PyInt_FromString,
36
33
    )
 
34
from cpython.tuple cimport (
 
35
    PyTuple_CheckExact,
 
36
    )
37
37
from cpython.list cimport (
38
38
    PyList_CheckExact,
39
39
    PyList_Append,
40
40
    )
41
 
from cpython.long cimport (
42
 
    PyLong_CheckExact,
 
41
from cpython.dict cimport (
 
42
    PyDict_CheckExact,
 
43
    )
 
44
from cpython.bool cimport (
 
45
    PyBool_Check,
43
46
    )
44
47
from cpython.mem cimport (
45
48
    PyMem_Free,
46
49
    PyMem_Malloc,
47
50
    PyMem_Realloc,
48
51
    )
49
 
from cpython.tuple cimport (
50
 
    PyTuple_CheckExact,
51
 
    )
52
52
 
53
53
from libc.stdlib cimport (
54
54
    strtol,
57
57
    memcpy,
58
58
    )
59
59
 
 
60
cdef extern from "Python.h":
 
61
    # There is no cython module for ceval.h for some reason
 
62
    int Py_GetRecursionLimit()
 
63
    int Py_EnterRecursiveCall(char *)
 
64
    void Py_LeaveRecursiveCall()
 
65
 
60
66
cdef extern from "python-compat.h":
61
67
    int snprintf(char* buffer, size_t nsize, char* fmt, ...)
62
 
    # Use wrapper with inverted error return so Cython can propogate
63
 
    int BrzPy_EnterRecursiveCall(char *) except 0
64
 
 
65
 
cdef extern from "Python.h":
66
 
    void Py_LeaveRecursiveCall()
67
68
 
68
69
cdef class Decoder
69
70
cdef class Encoder
113
114
        if 0 == self.size:
114
115
            raise ValueError('stream underflow')
115
116
 
116
 
        BrzPy_EnterRecursiveCall(" while bencode decoding")
 
117
        if Py_EnterRecursiveCall("_decode_object"):
 
118
            raise RuntimeError("too deeply nested")
117
119
        try:
118
120
            ch = self.tail[0]
119
121
            if c'0' <= ch <= c'9':
127
129
            elif ch == c'd':
128
130
                D_UPDATE_TAIL(self, 1)
129
131
                return self._decode_dict()
 
132
            else:
 
133
                raise ValueError('unknown object type identifier %r' % ch)
130
134
        finally:
131
135
            Py_LeaveRecursiveCall()
132
 
        raise ValueError('unknown object type identifier %r' % ch)
133
136
 
134
137
    cdef int _read_digits(self, char stop_char) except -1:
135
138
        cdef int i
339
342
        cdef Py_ssize_t x_len
340
343
        x_len = PyBytes_GET_SIZE(x)
341
344
        self._ensure_buffer(x_len + INT_BUF_SIZE)
342
 
        n = snprintf(self.tail, INT_BUF_SIZE, b'%ld:', x_len)
 
345
        n = snprintf(self.tail, INT_BUF_SIZE, b'%d:', x_len)
343
346
        if n < 0:
344
347
            raise MemoryError('string %s too big to encode' % x)
345
348
        memcpy(<void *>(self.tail+n), PyBytes_AS_STRING(x), x_len)
375
378
        E_UPDATE_TAIL(self, 1)
376
379
        return 1
377
380
 
378
 
    cpdef object process(self, object x):
379
 
        BrzPy_EnterRecursiveCall(" while bencode encoding")
 
381
    def process(self, object x):
 
382
        if Py_EnterRecursiveCall("encode"):
 
383
            raise RuntimeError("too deeply nested")
380
384
        try:
381
385
            if PyBytes_CheckExact(x):
382
386
                self._encode_string(x)