bencode-rs 0.0.2a4__tar.gz → 0.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/Cargo.lock +7 -0
  2. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/Cargo.toml +1 -0
  3. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/PKG-INFO +2 -1
  4. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/pyproject.toml +4 -1
  5. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/src/decode.rs +107 -91
  6. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/src/encode.rs +71 -44
  7. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/src/lib.rs +2 -0
  8. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/.coveragerc +0 -0
  9. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/.gitattributes +0 -0
  10. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/.github/renovate.json +0 -0
  11. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/.github/workflows/_build_wheels.yaml +0 -0
  12. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/.github/workflows/ci.yaml +0 -0
  13. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/.github/workflows/release.yaml +0 -0
  14. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/.gitignore +0 -0
  15. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/LICENSE +0 -0
  16. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/bencode_rs/__init__.py +0 -0
  17. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/bencode_rs/__init__.pyi +0 -0
  18. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/leak.py +0 -0
  19. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/readme.md +0 -0
  20. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/requirements.txt +0 -0
  21. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/rustfmt.toml +0 -0
  22. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/tests/fixtures/56507.torrent.bin +0 -0
  23. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/tests/fixtures/ubuntu-22.04.2-desktop-amd64.iso.torrent.bin +0 -0
  24. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/tests/test_1_encode.py +0 -0
  25. {bencode_rs-0.0.2a4 → bencode_rs-0.0.3}/tests/test_2_decode.py +0 -0
@@ -16,6 +16,7 @@ dependencies = [
16
16
  "itoa",
17
17
  "pyo3",
18
18
  "smallvec",
19
+ "syncpool",
19
20
  ]
20
21
 
21
22
  [[package]]
@@ -227,6 +228,12 @@ dependencies = [
227
228
  "unicode-ident",
228
229
  ]
229
230
 
231
+ [[package]]
232
+ name = "syncpool"
233
+ version = "0.1.6"
234
+ source = "registry+https://github.com/rust-lang/crates.io-index"
235
+ checksum = "276c1fc00f41ad03bdcf390e81f967adfdb9c828c0a967e1c92df8050471c6bd"
236
+
230
237
  [[package]]
231
238
  name = "target-lexicon"
232
239
  version = "0.12.14"
@@ -9,6 +9,7 @@ crate-type = ["cdylib"]
9
9
 
10
10
  [dependencies]
11
11
  bytes = "1.6.0"
12
+ syncpool = '0.1.6'
12
13
  itoa = "1.0.11"
13
14
  # pyo3 = { version = "0.21.1", features = ["extension-module", 'abi3'] }
14
15
  pyo3 = { version = "0.21.2", features = ['abi3-py38'] }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: bencode-rs
3
- Version: 0.0.2a4
3
+ Version: 0.0.3
4
4
  Classifier: Development Status :: 2 - Pre-Alpha
5
5
  Classifier: License :: OSI Approved :: MIT License
6
6
  Classifier: Programming Language :: Rust
@@ -10,6 +10,7 @@ Summary: a bencode parser binding build with pyo3
10
10
  Keywords: bittorrent,bencode,p2p
11
11
  Requires-Python: >=3.8, <4.0
12
12
  Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
13
+ Project-URL: Homepage, https://github.com/trim21/bencode-rs
13
14
 
14
15
  # A bencode serialize/deserialize library writte in Rust
15
16
 
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "bencode-rs"
7
- version = "0.0.2a4"
7
+ version = "0.0.3"
8
8
  requires-python = ">=3.8,<4.0"
9
9
  description = "a bencode parser binding build with pyo3"
10
10
  readme = "readme.md"
@@ -20,6 +20,9 @@ keywords = [
20
20
  "p2p",
21
21
  ]
22
22
 
23
+ [project.urls]
24
+ Homepage = 'https://github.com/trim21/bencode-rs'
25
+
23
26
  [tool.maturin]
24
27
  features = ["pyo3/extension-module"]
25
28
  module-name = "bencode_rs._bencode"
@@ -1,54 +1,106 @@
1
1
  use std::borrow::Cow;
2
- use std::collections::HashMap;
3
- use pyo3::ffi::{PyLong_FromString};
4
- use pyo3::prelude::*;
2
+
5
3
  use pyo3::{create_exception, PyResult, Python};
6
- use pyo3::exceptions::PyOverflowError;
7
- use pyo3::types::{PyBytes, PyDict, PyLong};
4
+ use pyo3::exceptions::PyTypeError;
5
+ use pyo3::ffi::PyLong_FromString;
6
+ use pyo3::prelude::*;
7
+ use pyo3::types::{PyBytes, PyDict};
8
8
 
9
9
  create_exception!(bencode_rs,BencodeDecodeError, pyo3::exceptions::PyException);
10
10
 
11
11
  type DecodeError = BencodeDecodeError;
12
12
 
13
13
  #[pyfunction]
14
- pub fn bdecode<'py>(py: Python<'py>, b: Bound<'py, PyBytes>) -> PyResult<Bound<'py, PyAny>> {
15
- if b.len().unwrap() == 0 {
16
- return Err(DecodeError::new_err("empty byte sequence"));
14
+ #[pyo3(text_signature = "(b: Bytes, /)")]
15
+ pub fn bdecode<'py>(py: Python<'py>, b: Py<PyAny>) -> PyResult<PyObject> {
16
+ let buf = match b.downcast_bound::<PyBytes>(py) {
17
+ Err(_) => {
18
+ return Err(PyTypeError::new_err("can only decode bytes"));
19
+ }
20
+ Ok(b) => b
21
+ };
22
+
23
+ if buf.len()? == 0 {
24
+ return Err(DecodeError::new_err("empty bytes"));
17
25
  }
18
26
 
19
27
  let mut ctx = Decoder {
20
- bytes: b.as_bytes().to_vec(),
28
+ bytes: buf.as_bytes(),
21
29
  index: 0,
22
30
  py,
23
- depth: 0,
31
+ // depth: 0,
24
32
  };
25
33
 
26
- return Ok(ctx.decode_any()?.into_bound(py));
34
+ return Ok(ctx.decode_any()?);
27
35
  }
28
36
 
29
37
 
30
- pub type Object = PyObject;
31
- pub type DecodeResult = Result<Object, PyErr>;
32
-
33
38
  struct Decoder<'a> {
34
39
  // str_key: bool,
35
- bytes: Vec<u8>,
40
+ bytes: &'a [u8],
36
41
  index: usize, // any torrent file larger than 4GiB?
37
42
  py: Python<'a>,
38
- depth: usize,
39
43
  }
40
44
 
41
45
  impl<'a> Decoder<'a> {
42
46
  fn decode_any(&mut self) -> Result<PyObject, PyErr> {
43
47
  return match self.current_byte()? {
44
48
  b'i' => self.decode_int(),
45
- b'0'..=b'9' => Ok(Cow::from(self.decode_bytes()?).into_py(self.py)),
46
- b'l' => self.decode_list(),
49
+ b'0'..=b'9' => {
50
+ let bytes = self.decode_bytes()?;
51
+
52
+ return Ok(Cow::from(bytes).into_py(self.py));
53
+ }
54
+ b'l' => {
55
+ let list = self.decode_list()?;
56
+
57
+ return Ok(list.into_any());
58
+ }
47
59
  b'd' => self.decode_dict(),
48
60
  _ => return Err(DecodeError::new_err("invalid leading byte")),
49
61
  };
50
62
  }
51
63
 
64
+ fn decode_bytes(&mut self) -> Result<Vec<u8>, PyErr> {
65
+ let index_sep = match self.bytes[self.index..].iter().position(|&b| b == b':') {
66
+ Some(i) => i,
67
+ None => {
68
+ return Err(DecodeError::new_err(format!(
69
+ "invalid bytes, missing length separator: index {}",
70
+ self.index
71
+ )));
72
+ }
73
+ } + self.index;
74
+
75
+ if self.bytes[self.index] == b'0' && self.index + 1 != index_sep {
76
+ return Err(DecodeError::new_err(format!(
77
+ "invalid bytes length, leading '0' found at index {}",
78
+ self.index
79
+ )));
80
+ }
81
+
82
+ let mut len: usize = 0;
83
+ for c in self.bytes[self.index..index_sep].iter() {
84
+ len = len * 10 + (c - b'0') as usize;
85
+ }
86
+
87
+ let bytes_start: usize = index_sep + 1;
88
+ let bytes_end: usize = bytes_start + len - 1;
89
+
90
+ if bytes_end > self.bytes.len() - 1 {
91
+ return Err(DecodeError::new_err(format!(
92
+ "invalid bytes length, buffer overflow to {}: index {}, len {}",
93
+ bytes_end, self.index, len
94
+ )));
95
+ }
96
+
97
+ self.index = bytes_end + 1;
98
+
99
+ let str_buff: Vec<u8> = self.bytes[bytes_start..=bytes_end].to_vec();
100
+
101
+ return Ok(str_buff);
102
+ }
103
+
52
104
  fn decode_int(&mut self) -> Result<PyObject, PyErr> {
53
105
  let index_e = match self.bytes[self.index..].iter().position(|&b| b == b'e') {
54
106
  Some(i) => i,
@@ -101,15 +153,35 @@ impl<'a> Decoder<'a> {
101
153
  }
102
154
 
103
155
  if sign < 0 {
104
- // slow path to build PyLong with python
105
- return self.decode_int_slow(index_e);
156
+ let mut val: i64 = 0;
157
+
158
+ for c_char in self.bytes[num_start..index_e].iter() {
159
+ let c = *c_char - b'0';
160
+ val = match val.checked_mul(10).and_then(|v| v.checked_add(c as i64)) {
161
+ Some(v) => v,
162
+ None => {
163
+ return self.decode_int_slow(index_e);
164
+ }
165
+ }
166
+ }
167
+
168
+ val = match val.checked_mul(-1) {
169
+ Some(v) => v,
170
+ None => {
171
+ // slow path to build PyLong with python
172
+ return self.decode_int_slow(index_e);
173
+ }
174
+ };
175
+
176
+ self.index = index_e + 1;
177
+ return Ok(val.into_py(self.py).into());
106
178
  }
107
179
 
108
- let mut val: u128 = 0;
180
+ let mut val: u64 = 0;
109
181
 
110
182
  for c_char in self.bytes[num_start..index_e].iter() {
111
183
  let c = *c_char - b'0';
112
- val = match val.checked_mul(10).and_then(|v| v.checked_add(c as u128)) {
184
+ val = match val.checked_mul(10).and_then(|v| v.checked_add(c as u64)) {
113
185
  Some(v) => v,
114
186
  None => {
115
187
  return self.decode_int_slow(index_e);
@@ -121,73 +193,23 @@ impl<'a> Decoder<'a> {
121
193
  return Ok(val.into_py(self.py).into());
122
194
  }
123
195
 
196
+ // support int may overflow i128/u128
124
197
  fn decode_int_slow(&mut self, index_e: usize) -> Result<PyObject, PyErr> {
125
198
  let s = self.bytes[self.index..index_e].to_vec();
126
199
 
127
200
  self.index = index_e + 1;
128
201
 
129
202
  let c_str = std::ffi::CString::new(s).unwrap();
203
+
130
204
  unsafe {
131
205
  let ptr = PyLong_FromString(c_str.as_ptr(), std::ptr::null_mut(), 10);
132
-
133
- // panic!("not implemented");
134
206
  return Py::from_owned_ptr_or_err(self.py, ptr);
135
207
  };
136
208
  }
137
209
 
138
- fn decode_bytes(&mut self) -> Result<Vec<u8>, PyErr> {
139
- let index_sep = match self.bytes[self.index..].iter().position(|&b| b == b':') {
140
- Some(i) => i,
141
- None => {
142
- return Err(DecodeError::new_err(format!(
143
- "invalid bytes, missing length separator: index {}",
144
- self.index
145
- )));
146
- }
147
- } + self.index;
148
-
149
- if self.bytes[self.index] == b'0' && self.index + 1 != index_sep {
150
- return Err(DecodeError::new_err(format!(
151
- "invalid bytes length, leading '0' found at index {}",
152
- self.index
153
- )));
154
- }
155
-
156
- let mut len: usize = 0;
157
- for c in self.bytes[self.index..index_sep].iter() {
158
- len = len * 10 + (c - b'0') as usize;
159
- }
160
-
161
- if index_sep + len >= self.bytes.len() {
162
- return Err(DecodeError::new_err(format!(
163
- "invalid bytes length, index out of range: index {}, len {}",
164
- self.index, len
165
- )));
166
- }
167
-
168
- let str_buff = self.bytes[index_sep + 1..index_sep + 1 + len].to_vec();
169
-
170
- self.index = index_sep + len + 1;
171
-
172
- // let o = PyBytes::new_bound(self.py, &str_buff);
173
-
174
- return Ok(str_buff);
175
- }
176
-
177
- // fn check_depth(&mut self) -> Result<(), PyErr> {
178
- // if self.depth > 10000 {
179
- // return Err(DecodeError::new_err("object depth limit 10000 reached"));
180
- // }
181
- //
182
- // return Ok(());
183
- // }
184
-
185
- fn decode_list(&mut self) -> DecodeResult {
186
- // self.depth += 1;
187
- // self.check_depth()?;
188
-
210
+ fn decode_list(&mut self) -> PyResult<PyObject> {
189
211
  self.index += 1;
190
- let mut l = Vec::with_capacity(8);
212
+ let mut l = Vec::<PyObject>::with_capacity(16);
191
213
 
192
214
  loop {
193
215
  match self.bytes.get(self.index) {
@@ -201,17 +223,16 @@ impl<'a> Decoder<'a> {
201
223
  }
202
224
  }
203
225
 
204
- // self.depth -= 1;
205
226
  self.index += 1;
206
- return Ok(l.into_py(self.py));
227
+
228
+
229
+ Ok(l.into_py(self.py))
207
230
  }
208
231
 
209
- fn decode_dict(&mut self) -> DecodeResult {
210
- // self.depth += 1;
211
- // self.check_depth()?;
232
+ fn decode_dict(&mut self) -> Result<PyObject, PyErr> {
212
233
  self.index += 1;
213
234
 
214
- let mut map: HashMap<Cow<[u8]>, Object> = HashMap::with_capacity(8);
235
+ let d = PyDict::new_bound(self.py);
215
236
  let mut last_key: Option<Cow<[u8]>> = None;
216
237
  loop {
217
238
  match self.bytes.get(self.index) {
@@ -222,7 +243,8 @@ impl<'a> Decoder<'a> {
222
243
  Some(_) => {
223
244
  let key = self.decode_bytes()?;
224
245
  let value = self.decode_any()?;
225
- let ck = Cow::from(key.clone());
246
+
247
+ let ck = Cow::from(key);
226
248
  if let Some(lk) = last_key {
227
249
  if lk > ck {
228
250
  return Err(DecodeError::new_err(format!("dict key not sorted. index {}", self.index)));
@@ -230,19 +252,13 @@ impl<'a> Decoder<'a> {
230
252
  return Err(DecodeError::new_err(format!("duplicated dict key found: index {}", self.index)));
231
253
  }
232
254
  }
233
- map.insert(ck.clone(), value);
255
+ d.set_item(ck.clone().into_py(self.py), value.into_py(self.py))?;
256
+ // map.insert(ck.clone(), value);
234
257
  last_key = Some(ck);
235
258
  }
236
259
  }
237
260
  }
238
261
 
239
- let d = PyDict::new_bound(self.py);
240
-
241
- for (k, v) in map.iter_mut() {
242
- d.set_item(k.to_owned().into_py(self.py), v.to_owned())?;
243
- }
244
-
245
- // self.depth -= 1;
246
262
  self.index += 1;
247
263
  return Ok(d.into_py(self.py));
248
264
  }
@@ -1,36 +1,75 @@
1
- use std::borrow::Cow;
2
1
  use std::collections::HashSet;
3
2
 
4
- use bytes::{BufMut, Bytes, BytesMut};
3
+ use bytes::{BufMut, BytesMut};
4
+ use pyo3::{create_exception, exceptions::PyTypeError, prelude::*, types::{PyBool, PyBytes, PyDict, PyInt, PyList, PyString, PyTuple}};
5
5
  use pyo3::exceptions::PyValueError;
6
- use pyo3::{create_exception, exceptions::PyTypeError, ffi::Py_TYPE, prelude::*, types::{PyBool, PyBytes, PyDict, PyInt, PyList, PyString, PyTuple, PyType}};
7
6
  use smallvec::SmallVec;
7
+ use syncpool::SyncPool;
8
8
 
9
9
  create_exception!(bencode_rs, BencodeEncodeError, pyo3::exceptions::PyException);
10
10
 
11
- type EncodeError = BencodeEncodeError;
11
+ pub const MIB: usize = 1_048_576;
12
12
 
13
- struct Context<'py> {
14
- buf: BytesMut,
15
- seen: HashSet<usize>,
16
- py: Python<'py>,
13
+ pub fn init() {
14
+ unsafe {
15
+ CONTEXT_POOL.replace(SyncPool::with_builder(Context::initializer));
16
+ }
17
17
  }
18
18
 
19
19
  #[pyfunction]
20
20
  #[pyo3(text_signature = "(v: Any, /)")]
21
- pub fn bencode<'py>(py: Python<'py>, v: Bound<'py, PyAny>) -> PyResult<Cow<'py, [u8]>> {
22
- let mut ctx = Context {
23
- buf: BytesMut::with_capacity(4096),
24
- seen: HashSet::with_capacity(100),
25
- py,
26
- };
21
+ pub fn bencode<'py>(py: Python<'py>, v: Bound<'py, PyAny>) -> PyResult<Bound<'py, PyBytes>> {
22
+ let mut ctx = get_ctx();
23
+ // let mut ctx = Context::initializer();
24
+
25
+ encode_any(&mut ctx, py, v)?;
26
+
27
+ let r = PyBytes::new_bound(py, ctx.buf.as_ref());
28
+
29
+ release_ctx(ctx);
30
+
31
+ return Ok(r);
32
+ }
33
+
34
+ type EncodeError = BencodeEncodeError;
35
+
36
+ static mut CONTEXT_POOL: Option<SyncPool<Context>> = None;
37
+
38
+ fn get_ctx() -> Box<Context> {
39
+ unsafe {
40
+ CONTEXT_POOL.as_mut().unwrap().get()
41
+ }
42
+ }
43
+
44
+ fn release_ctx(mut ctx: Box<Context>) -> () {
45
+ // do not store large buffers
46
+ // who encode torrent >= 100 MiB normally?
47
+ if ctx.buf.capacity() > 100 * MIB {
48
+ return;
49
+ }
50
+ ctx.buf.clear();
51
+ ctx.seen.clear();
52
+ unsafe {
53
+ CONTEXT_POOL.as_mut().unwrap().put(ctx);
54
+ }
55
+ }
56
+
27
57
 
28
- encode_any(&mut ctx, v)?;
58
+ struct Context {
59
+ buf: BytesMut,
60
+ seen: HashSet<usize>,
61
+ }
29
62
 
30
- return Ok(ctx.buf.to_vec().into());
63
+ impl Context {
64
+ fn initializer() -> Self {
65
+ Self {
66
+ buf: BytesMut::with_capacity(4096),
67
+ seen: HashSet::with_capacity(100),
68
+ }
69
+ }
31
70
  }
32
71
 
33
- fn encode_any<'py>(ctx: &mut Context, value: Bound<'py, PyAny>) -> PyResult<()> {
72
+ fn encode_any<'py>(ctx: &mut Context, py: Python<'py>, value: Bound<'py, PyAny>) -> PyResult<()> {
34
73
  if let Ok(s) = value.downcast::<PyString>() {
35
74
  let str = s.to_string();
36
75
  let mut buffer = itoa::Buffer::new();
@@ -87,7 +126,7 @@ fn encode_any<'py>(ctx: &mut Context, value: Bound<'py, PyAny>) -> PyResult<()>
87
126
  ctx.buf.put_u8(b'l');
88
127
 
89
128
  for x in seq {
90
- encode_any(ctx, x)?;
129
+ encode_any(ctx, py, x)?;
91
130
  }
92
131
 
93
132
  ctx.buf.put_u8(b'e');
@@ -108,7 +147,7 @@ fn encode_any<'py>(ctx: &mut Context, value: Bound<'py, PyAny>) -> PyResult<()>
108
147
  ctx.buf.put_u8(b'l');
109
148
 
110
149
  for x in seq {
111
- encode_any(ctx, x)?;
150
+ encode_any(ctx, py, x)?;
112
151
  }
113
152
 
114
153
  ctx.buf.put_u8(b'e');
@@ -127,7 +166,7 @@ fn encode_any<'py>(ctx: &mut Context, value: Bound<'py, PyAny>) -> PyResult<()>
127
166
  }
128
167
  ctx.seen.insert(ptr);
129
168
 
130
- _encode_dict(ctx, dict)?;
169
+ encode_dict(ctx, py, dict)?;
131
170
 
132
171
  ctx.seen.remove(&ptr);
133
172
  return Ok(());
@@ -139,16 +178,6 @@ fn encode_any<'py>(ctx: &mut Context, value: Bound<'py, PyAny>) -> PyResult<()>
139
178
  return Err(PyTypeError::new_err(format!("Unsupported type '{name}'")));
140
179
  }
141
180
 
142
- fn _encode_bytes(v: Cow<[u8]>, buf: &mut BytesMut) -> PyResult<()> {
143
- let mut buffer = itoa::Buffer::new();
144
-
145
- buf.put(buffer.format(v.len()).as_bytes());
146
- buf.put_u8(b':');
147
- buf.put(Bytes::from(v.to_vec()));
148
-
149
- return Ok(());
150
- }
151
-
152
181
  #[inline]
153
182
  fn _encode_str<'py>(v: String, buf: &mut BytesMut) -> PyResult<()> {
154
183
  let mut buffer = itoa::Buffer::new();
@@ -160,7 +189,7 @@ fn _encode_str<'py>(v: String, buf: &mut BytesMut) -> PyResult<()> {
160
189
  return Ok(());
161
190
  }
162
191
 
163
- fn _encode_dict<'py>(ctx: &mut Context, v: &Bound<'py, PyDict>) -> PyResult<()> {
192
+ fn encode_dict<'py>(ctx: &mut Context, py: Python<'py>, v: &Bound<'py, PyDict>) -> PyResult<()> {
164
193
  ctx.buf.put_u8(b'd');
165
194
 
166
195
  let mut sv: SmallVec<[(String, Bound<'_, PyAny>); 8]> = SmallVec::with_capacity(v.len());
@@ -168,23 +197,21 @@ fn _encode_dict<'py>(ctx: &mut Context, v: &Bound<'py, PyDict>) -> PyResult<()>
168
197
  for item in v.items().iter() {
169
198
  let (key, value): (PyObject, Bound<'_, PyAny>) = item.extract()?;
170
199
 
171
- if let Ok(d) = key.extract::<&PyString>(ctx.py) {
200
+ if let Ok(d) = key.extract::<&PyString>(py) {
172
201
  let bb = d.to_string();
173
202
  sv.push((bb, value));
174
- } else if let Ok(d) = key.extract::<&PyBytes>(ctx.py) {
203
+ continue;
204
+ }
205
+
206
+ if let Ok(d) = key.extract::<&PyBytes>(py) {
175
207
  sv.push((String::from_utf8(d.as_bytes().into())?, value));
176
- } else {
177
- unsafe {
178
- let typ = Py_TYPE(key.as_ptr());
208
+ continue;
209
+ }
179
210
 
180
- let bb = PyType::from_borrowed_type_ptr(ctx.py, typ);
181
- let name = bb.qualname()?;
211
+ let typ = value.get_type();
212
+ let name = typ.name()?;
182
213
 
183
- return Err(PyTypeError::new_err(format!(
184
- "Unsupported type {name} as dict keys"
185
- )));
186
- }
187
- }
214
+ return Err(PyTypeError::new_err(format!("Unsupported type '{name}' as dict key")));
188
215
  }
189
216
 
190
217
  sv.sort_unstable_by(|a, b| a.0.cmp(&b.0));
@@ -204,7 +231,7 @@ fn _encode_dict<'py>(ctx: &mut Context, v: &Bound<'py, PyDict>) -> PyResult<()>
204
231
 
205
232
  for (key, value) in sv {
206
233
  _encode_str(key, &mut ctx.buf)?;
207
- encode_any(ctx, value.into_any())?;
234
+ encode_any(ctx, py, value.into_any())?;
208
235
  }
209
236
 
210
237
  ctx.buf.put_u8(b'e');
@@ -8,6 +8,8 @@ use pyo3::prelude::*;
8
8
 
9
9
  #[pymodule]
10
10
  fn _bencode(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
11
+ encode::init();
12
+
11
13
  m.add_function(wrap_pyfunction!(encode::bencode, m)?)?;
12
14
  m.add_function(wrap_pyfunction!(decode::bdecode, m)?)?;
13
15
  m.add(
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes