bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
4763.2.4
by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry. |
1 |
# Copyright (C) 2007, 2009, 2010 Canonical Ltd
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
|
4398.5.20
by John Arbash Meinel
Fix the GPL header |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
16 |
|
17 |
"""Pyrex implementation for bencode coder/decoder"""
|
|
18 |
||
|
6665.1.1
by Jelmer Vernooij
Drop pyrex support. |
19 |
from __future__ import absolute_import |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
20 |
|
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
21 |
cdef extern from "stddef.h": |
22 |
ctypedef unsigned int size_t |
|
23 |
||
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
24 |
cdef extern from "Python.h": |
25 |
ctypedef int Py_ssize_t |
|
26 |
int PyInt_CheckExact(object o) |
|
27 |
int PyLong_CheckExact(object o) |
|
28 |
int PyString_CheckExact(object o) |
|
29 |
int PyTuple_CheckExact(object o) |
|
30 |
int PyList_CheckExact(object o) |
|
31 |
int PyDict_CheckExact(object o) |
|
32 |
int PyBool_Check(object o) |
|
33 |
object PyString_FromStringAndSize(char *v, Py_ssize_t len) |
|
|
2694.5.12
by Jelmer Vernooij
Avoid using snprintf for strings, use memcpy instead. |
34 |
char *PyString_AS_STRING(object o) except NULL |
35 |
Py_ssize_t PyString_GET_SIZE(object o) except -1 |
|
|
2694.5.17
by Jelmer Vernooij
Avoid using malloc in the inner loop. |
36 |
object PyInt_FromString(char *str, char **pend, int base) |
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
37 |
int Py_GetRecursionLimit() |
38 |
int Py_EnterRecursiveCall(char *) |
|
39 |
void Py_LeaveRecursiveCall() |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
40 |
|
|
4398.5.12
by John Arbash Meinel
One of the biggest wins to date, use PyList_Append directly. |
41 |
int PyList_Append(object, object) except -1 |
42 |
||
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
43 |
cdef extern from "stdlib.h": |
44 |
void free(void *memblock) |
|
45 |
void *malloc(size_t size) |
|
46 |
void *realloc(void *memblock, size_t size) |
|
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
47 |
long strtol(char *, char **, int) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
48 |
|
49 |
cdef extern from "string.h": |
|
50 |
void *memcpy(void *dest, void *src, size_t count) |
|
51 |
||
|
2694.5.11
by Jelmer Vernooij
Use global python-compat. |
52 |
cdef extern from "python-compat.h": |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
53 |
int snprintf(char* buffer, size_t nsize, char* fmt, ...) |
54 |
||
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
55 |
cdef class Decoder |
56 |
cdef class Encoder |
|
|
4398.5.10
by John Arbash Meinel
Move self._update_tail into a macro for UPDATE_TAIL. |
57 |
|
58 |
cdef extern from "_bencode_pyx.h": |
|
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
59 |
void D_UPDATE_TAIL(Decoder, int n) |
60 |
void E_UPDATE_TAIL(Encoder, int n) |
|
61 |
||
|
6665.1.1
by Jelmer Vernooij
Drop pyrex support. |
62 |
from ._static_tuple_c cimport StaticTuple, StaticTuple_CheckExact, \ |
|
4679.8.10
by John Arbash Meinel
quick patch to allow bencode to handle StaticTuple objects. |
63 |
import_static_tuple_c
|
64 |
||
65 |
import_static_tuple_c() |
|
66 |
||
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
67 |
|
68 |
cdef class Decoder: |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
69 |
"""Bencode decoder""" |
70 |
||
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
71 |
cdef readonly char *tail |
72 |
cdef readonly int size |
|
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
73 |
cdef readonly int _yield_tuples |
|
2694.5.21
by Jelmer Vernooij
Review feedback from Alexander. |
74 |
cdef object text |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
75 |
|
|
2694.5.5
by Jelmer Vernooij
Support bdecode_as_tuple. |
76 |
def __init__(self, s, yield_tuples=0): |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
77 |
"""Initialize decoder engine. |
78 |
@param s: Python string.
|
|
79 |
"""
|
|
80 |
if not PyString_CheckExact(s): |
|
|
2694.5.9
by Jelmer Vernooij
Fix tests. |
81 |
raise TypeError("String required") |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
82 |
|
|
2694.5.21
by Jelmer Vernooij
Review feedback from Alexander. |
83 |
self.text = s |
|
2694.5.12
by Jelmer Vernooij
Avoid using snprintf for strings, use memcpy instead. |
84 |
self.tail = PyString_AS_STRING(s) |
85 |
self.size = PyString_GET_SIZE(s) |
|
|
2694.5.5
by Jelmer Vernooij
Support bdecode_as_tuple. |
86 |
self._yield_tuples = int(yield_tuples) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
87 |
|
88 |
def decode(self): |
|
|
4398.5.11
by John Arbash Meinel
Turn Decoder.decode_object into _decode_object. |
89 |
result = self._decode_object() |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
90 |
if self.size != 0: |
91 |
raise ValueError('junk in stream') |
|
92 |
return result |
|
93 |
||
94 |
def decode_object(self): |
|
|
4398.5.11
by John Arbash Meinel
Turn Decoder.decode_object into _decode_object. |
95 |
return self._decode_object() |
96 |
||
97 |
cdef object _decode_object(self): |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
98 |
cdef char ch |
99 |
||
100 |
if 0 == self.size: |
|
101 |
raise ValueError('stream underflow') |
|
102 |
||
|
4398.5.11
by John Arbash Meinel
Turn Decoder.decode_object into _decode_object. |
103 |
if Py_EnterRecursiveCall("_decode_object"): |
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
104 |
raise RuntimeError("too deeply nested") |
105 |
try: |
|
106 |
ch = self.tail[0] |
|
|
4398.5.14
by John Arbash Meinel
Some small tweaks to decoding strings (avoid passing over the length 2x) |
107 |
if c'0' <= ch <= c'9': |
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
108 |
return self._decode_string() |
109 |
elif ch == c'l': |
|
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
110 |
D_UPDATE_TAIL(self, 1) |
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
111 |
return self._decode_list() |
|
4398.5.14
by John Arbash Meinel
Some small tweaks to decoding strings (avoid passing over the length 2x) |
112 |
elif ch == c'i': |
113 |
D_UPDATE_TAIL(self, 1) |
|
114 |
return self._decode_int() |
|
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
115 |
elif ch == c'd': |
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
116 |
D_UPDATE_TAIL(self, 1) |
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
117 |
return self._decode_dict() |
118 |
else: |
|
119 |
raise ValueError('unknown object type identifier %r' % ch) |
|
120 |
finally: |
|
121 |
Py_LeaveRecursiveCall() |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
122 |
|
|
2694.5.17
by Jelmer Vernooij
Avoid using malloc in the inner loop. |
123 |
cdef int _read_digits(self, char stop_char) except -1: |
124 |
cdef int i |
|
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
125 |
i = 0 |
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
126 |
while ((self.tail[i] >= c'0' and self.tail[i] <= c'9') or |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
127 |
self.tail[i] == c'-') and i < self.size: |
|
2694.5.18
by Alexander Belchenko
Fix pyrex compatibility. |
128 |
i = i + 1 |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
129 |
|
130 |
if self.tail[i] != stop_char: |
|
131 |
raise ValueError("Stop character %c not found: %c" % |
|
132 |
(stop_char, self.tail[i])) |
|
133 |
if (self.tail[0] == c'0' or |
|
134 |
(self.tail[0] == c'-' and self.tail[1] == c'0')): |
|
135 |
if i == 1: |
|
|
2694.5.17
by Jelmer Vernooij
Avoid using malloc in the inner loop. |
136 |
return i |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
137 |
else: |
138 |
raise ValueError # leading zeroes are not allowed |
|
|
2694.5.17
by Jelmer Vernooij
Avoid using malloc in the inner loop. |
139 |
return i |
140 |
||
141 |
cdef object _decode_int(self): |
|
142 |
cdef int i |
|
143 |
i = self._read_digits(c'e') |
|
|
2694.5.21
by Jelmer Vernooij
Review feedback from Alexander. |
144 |
self.tail[i] = 0 |
145 |
try: |
|
146 |
ret = PyInt_FromString(self.tail, NULL, 10) |
|
147 |
finally: |
|
148 |
self.tail[i] = c'e' |
|
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
149 |
D_UPDATE_TAIL(self, i+1) |
|
2694.5.16
by Jelmer Vernooij
Simplify the code a bit more. |
150 |
return ret |
151 |
||
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
152 |
cdef object _decode_string(self): |
|
4398.5.14
by John Arbash Meinel
Some small tweaks to decoding strings (avoid passing over the length 2x) |
153 |
cdef int n |
154 |
cdef char *next_tail |
|
155 |
# strtol allows leading whitespace, negatives, and leading zeros
|
|
156 |
# however, all callers have already checked that '0' <= tail[0] <= '9'
|
|
157 |
# or they wouldn't have called _decode_string
|
|
158 |
# strtol will stop at trailing whitespace, etc
|
|
159 |
n = strtol(self.tail, &next_tail, 10) |
|
160 |
if next_tail == NULL or next_tail[0] != c':': |
|
161 |
raise ValueError('string len not terminated by ":"') |
|
162 |
# strtol allows leading zeros, so validate that we don't have that
|
|
163 |
if (self.tail[0] == c'0' |
|
164 |
and (n != 0 or (next_tail - self.tail != 1))): |
|
165 |
raise ValueError('leading zeros are not allowed') |
|
166 |
D_UPDATE_TAIL(self, next_tail - self.tail + 1) |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
167 |
if n == 0: |
168 |
return '' |
|
169 |
if n > self.size: |
|
170 |
raise ValueError('stream underflow') |
|
|
2694.5.13
by Jelmer Vernooij
Always checks for strings first since they're more common, make sure sizes of strings are never below zero. |
171 |
if n < 0: |
172 |
raise ValueError('string size below zero: %d' % n) |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
173 |
|
174 |
result = PyString_FromStringAndSize(self.tail, n) |
|
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
175 |
D_UPDATE_TAIL(self, n) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
176 |
return result |
177 |
||
178 |
cdef object _decode_list(self): |
|
179 |
result = [] |
|
180 |
||
181 |
while self.size > 0: |
|
|
2694.5.7
by Jelmer Vernooij
use C character constants rather than a custom enum. |
182 |
if self.tail[0] == c'e': |
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
183 |
D_UPDATE_TAIL(self, 1) |
|
2694.5.5
by Jelmer Vernooij
Support bdecode_as_tuple. |
184 |
if self._yield_tuples: |
185 |
return tuple(result) |
|
186 |
else: |
|
187 |
return result |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
188 |
else: |
|
4398.5.14
by John Arbash Meinel
Some small tweaks to decoding strings (avoid passing over the length 2x) |
189 |
# As a quick shortcut, check to see if the next object is a
|
190 |
# string, since we know that won't be creating recursion
|
|
191 |
# if self.tail[0] >= c'0' and self.tail[0] <= c'9':
|
|
|
4398.5.12
by John Arbash Meinel
One of the biggest wins to date, use PyList_Append directly. |
192 |
PyList_Append(result, self._decode_object()) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
193 |
|
194 |
raise ValueError('malformed list') |
|
195 |
||
196 |
cdef object _decode_dict(self): |
|
197 |
cdef char ch |
|
198 |
||
199 |
result = {} |
|
200 |
lastkey = None |
|
201 |
||
202 |
while self.size > 0: |
|
203 |
ch = self.tail[0] |
|
|
2694.5.7
by Jelmer Vernooij
use C character constants rather than a custom enum. |
204 |
if ch == c'e': |
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
205 |
D_UPDATE_TAIL(self, 1) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
206 |
return result |
|
2694.5.15
by Jelmer Vernooij
Simplify dict parsing. |
207 |
else: |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
208 |
# keys should be strings only
|
|
4398.5.14
by John Arbash Meinel
Some small tweaks to decoding strings (avoid passing over the length 2x) |
209 |
if self.tail[0] < c'0' or self.tail[0] > c'9': |
210 |
raise ValueError('key was not a simple string.') |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
211 |
key = self._decode_string() |
212 |
if lastkey >= key: |
|
213 |
raise ValueError('dict keys disordered') |
|
214 |
else: |
|
215 |
lastkey = key |
|
|
4398.5.11
by John Arbash Meinel
Turn Decoder.decode_object into _decode_object. |
216 |
value = self._decode_object() |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
217 |
result[key] = value |
218 |
||
219 |
raise ValueError('malformed dict') |
|
220 |
||
221 |
||
222 |
def bdecode(object s): |
|
223 |
"""Decode string x to Python object""" |
|
224 |
return Decoder(s).decode() |
|
225 |
||
226 |
||
|
2694.5.5
by Jelmer Vernooij
Support bdecode_as_tuple. |
227 |
def bdecode_as_tuple(object s): |
228 |
"""Decode string x to Python object, using tuples rather than lists.""" |
|
229 |
return Decoder(s, True).decode() |
|
230 |
||
231 |
||
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
232 |
class Bencached(object): |
233 |
__slots__ = ['bencoded'] |
|
234 |
||
235 |
def __init__(self, s): |
|
236 |
self.bencoded = s |
|
237 |
||
238 |
||
239 |
cdef enum: |
|
240 |
INITSIZE = 1024 # initial size for encoder buffer |
|
|
2694.5.22
by Jelmer Vernooij
Review feedback from bialix: |
241 |
INT_BUF_SIZE = 32 |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
242 |
|
243 |
||
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
244 |
cdef class Encoder: |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
245 |
"""Bencode encoder""" |
246 |
||
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
247 |
cdef readonly char *tail |
248 |
cdef readonly int size |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
249 |
cdef readonly char *buffer |
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
250 |
cdef readonly int maxsize |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
251 |
|
252 |
def __init__(self, int maxsize=INITSIZE): |
|
253 |
"""Initialize encoder engine |
|
254 |
@param maxsize: initial size of internal char buffer
|
|
255 |
"""
|
|
256 |
cdef char *p |
|
257 |
||
258 |
self.maxsize = 0 |
|
259 |
self.size = 0 |
|
260 |
self.tail = NULL |
|
261 |
||
262 |
p = <char*>malloc(maxsize) |
|
263 |
if p == NULL: |
|
|
2694.5.6
by Jelmer Vernooij
Use MemoryError rather than custom exception. |
264 |
raise MemoryError('Not enough memory to allocate buffer ' |
265 |
'for encoder') |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
266 |
self.buffer = p |
267 |
self.maxsize = maxsize |
|
268 |
self.tail = p |
|
269 |
||
|
4634.112.1
by John Arbash Meinel
bencode.Encoder should use __dealloc__ to free C level resources. |
270 |
def __dealloc__(self): |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
271 |
free(self.buffer) |
272 |
self.buffer = NULL |
|
273 |
self.maxsize = 0 |
|
274 |
||
275 |
def __str__(self): |
|
276 |
if self.buffer != NULL and self.size != 0: |
|
277 |
return PyString_FromStringAndSize(self.buffer, self.size) |
|
278 |
else: |
|
279 |
return '' |
|
280 |
||
281 |
cdef int _ensure_buffer(self, int required) except 0: |
|
282 |
"""Ensure that tail of CharTail buffer has enough size. |
|
283 |
If buffer is not big enough then function try to
|
|
284 |
realloc buffer.
|
|
285 |
"""
|
|
286 |
cdef char *new_buffer |
|
287 |
cdef int new_size |
|
288 |
||
289 |
if self.size + required < self.maxsize: |
|
290 |
return 1 |
|
291 |
||
292 |
new_size = self.maxsize |
|
293 |
while new_size < self.size + required: |
|
294 |
new_size = new_size * 2 |
|
295 |
new_buffer = <char*>realloc(self.buffer, <size_t>new_size) |
|
296 |
if new_buffer == NULL: |
|
|
2694.5.6
by Jelmer Vernooij
Use MemoryError rather than custom exception. |
297 |
raise MemoryError('Cannot realloc buffer for encoder') |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
298 |
|
299 |
self.buffer = new_buffer |
|
300 |
self.maxsize = new_size |
|
301 |
self.tail = &new_buffer[self.size] |
|
302 |
return 1 |
|
303 |
||
304 |
cdef int _encode_int(self, int x) except 0: |
|
305 |
"""Encode int to bencode string iNNNe |
|
306 |
@param x: value to encode
|
|
307 |
"""
|
|
308 |
cdef int n |
|
|
2694.5.22
by Jelmer Vernooij
Review feedback from bialix: |
309 |
self._ensure_buffer(INT_BUF_SIZE) |
310 |
n = snprintf(self.tail, INT_BUF_SIZE, "i%de", x) |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
311 |
if n < 0: |
|
2694.5.6
by Jelmer Vernooij
Use MemoryError rather than custom exception. |
312 |
raise MemoryError('int %d too big to encode' % x) |
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
313 |
E_UPDATE_TAIL(self, n) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
314 |
return 1 |
315 |
||
316 |
cdef int _encode_long(self, x) except 0: |
|
317 |
return self._append_string(''.join(('i', str(x), 'e'))) |
|
318 |
||
319 |
cdef int _append_string(self, s) except 0: |
|
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
320 |
cdef Py_ssize_t n |
321 |
n = PyString_GET_SIZE(s) |
|
322 |
self._ensure_buffer(n) |
|
323 |
memcpy(self.tail, PyString_AS_STRING(s), n) |
|
324 |
E_UPDATE_TAIL(self, n) |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
325 |
return 1 |
326 |
||
327 |
cdef int _encode_string(self, x) except 0: |
|
328 |
cdef int n |
|
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
329 |
cdef Py_ssize_t x_len |
330 |
x_len = PyString_GET_SIZE(x) |
|
331 |
self._ensure_buffer(x_len + INT_BUF_SIZE) |
|
332 |
n = snprintf(self.tail, INT_BUF_SIZE, '%d:', x_len) |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
333 |
if n < 0: |
|
2694.5.6
by Jelmer Vernooij
Use MemoryError rather than custom exception. |
334 |
raise MemoryError('string %s too big to encode' % x) |
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
335 |
memcpy(<void *>(self.tail+n), PyString_AS_STRING(x), x_len) |
336 |
E_UPDATE_TAIL(self, n + x_len) |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
337 |
return 1 |
338 |
||
339 |
cdef int _encode_list(self, x) except 0: |
|
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
340 |
self._ensure_buffer(1) |
|
2694.5.7
by Jelmer Vernooij
use C character constants rather than a custom enum. |
341 |
self.tail[0] = c'l' |
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
342 |
E_UPDATE_TAIL(self, 1) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
343 |
|
344 |
for i in x: |
|
345 |
self.process(i) |
|
346 |
||
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
347 |
self._ensure_buffer(1) |
|
2694.5.7
by Jelmer Vernooij
use C character constants rather than a custom enum. |
348 |
self.tail[0] = c'e' |
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
349 |
E_UPDATE_TAIL(self, 1) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
350 |
return 1 |
351 |
||
352 |
cdef int _encode_dict(self, x) except 0: |
|
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
353 |
self._ensure_buffer(1) |
|
2694.5.7
by Jelmer Vernooij
use C character constants rather than a custom enum. |
354 |
self.tail[0] = c'd' |
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
355 |
E_UPDATE_TAIL(self, 1) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
356 |
|
357 |
keys = x.keys() |
|
358 |
keys.sort() |
|
359 |
for k in keys: |
|
360 |
if not PyString_CheckExact(k): |
|
361 |
raise TypeError('key in dict should be string') |
|
362 |
self._encode_string(k) |
|
363 |
self.process(x[k]) |
|
364 |
||
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
365 |
self._ensure_buffer(1) |
|
2694.5.7
by Jelmer Vernooij
use C character constants rather than a custom enum. |
366 |
self.tail[0] = c'e' |
|
4398.5.13
by John Arbash Meinel
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail. |
367 |
E_UPDATE_TAIL(self, 1) |
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
368 |
return 1 |
369 |
||
370 |
def process(self, object x): |
|
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
371 |
if Py_EnterRecursiveCall("encode"): |
372 |
raise RuntimeError("too deeply nested") |
|
373 |
try: |
|
374 |
if PyString_CheckExact(x): |
|
375 |
self._encode_string(x) |
|
376 |
elif PyInt_CheckExact(x): |
|
377 |
self._encode_int(x) |
|
378 |
elif PyLong_CheckExact(x): |
|
379 |
self._encode_long(x) |
|
|
4679.8.10
by John Arbash Meinel
quick patch to allow bencode to handle StaticTuple objects. |
380 |
elif (PyList_CheckExact(x) or PyTuple_CheckExact(x) |
381 |
or StaticTuple_CheckExact(x)): |
|
|
2694.5.20
by Jelmer Vernooij
add handling of deep nesting. |
382 |
self._encode_list(x) |
383 |
elif PyDict_CheckExact(x): |
|
384 |
self._encode_dict(x) |
|
385 |
elif PyBool_Check(x): |
|
386 |
self._encode_int(int(x)) |
|
387 |
elif isinstance(x, Bencached): |
|
388 |
self._append_string(x.bencoded) |
|
389 |
else: |
|
390 |
raise TypeError('unsupported type %r' % x) |
|
391 |
finally: |
|
392 |
Py_LeaveRecursiveCall() |
|
|
2694.5.1
by Alexander Belchenko
pyrex bencode (without benchmarks) |
393 |
|
394 |
||
395 |
def bencode(x): |
|
396 |
"""Encode Python object x to string""" |
|
397 |
encoder = Encoder() |
|
398 |
encoder.process(x) |
|
399 |
return str(encoder) |