/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

  • Committer: Jelmer Vernooij
  • Date: 2018-08-21 19:28:52 UTC
  • mfrom: (7078 work)
  • mto: This revision was merged to the branch mainline in revision 7101.
  • Revision ID: jelmer@jelmer.uk-20180821192852-ipy6m8q95bbst223
Merge trunk.

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