bencode-rs 0.0.2a3__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 (28) hide show
  1. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/.github/workflows/_build_wheels.yaml +0 -12
  2. bencode_rs-0.0.3/.github/workflows/ci.yaml +79 -0
  3. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/.github/workflows/release.yaml +4 -5
  4. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/Cargo.lock +7 -0
  5. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/Cargo.toml +1 -0
  6. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/PKG-INFO +3 -6
  7. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/pyproject.toml +3 -9
  8. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/readme.md +1 -1
  9. bencode_rs-0.0.3/requirements.txt +8 -0
  10. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/src/decode.rs +113 -97
  11. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/src/encode.rs +71 -44
  12. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/src/lib.rs +2 -0
  13. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/tests/test_2_decode.py +1 -0
  14. bencode_rs-0.0.2a3/.github/workflows/build.yaml +0 -35
  15. bencode_rs-0.0.2a3/.github/workflows/tests.yaml +0 -45
  16. bencode_rs-0.0.2a3/bench.py +0 -53
  17. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/.coveragerc +0 -0
  18. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/.gitattributes +0 -0
  19. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/.github/renovate.json +0 -0
  20. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/.gitignore +0 -0
  21. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/LICENSE +0 -0
  22. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/bencode_rs/__init__.py +0 -0
  23. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/bencode_rs/__init__.pyi +0 -0
  24. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/leak.py +0 -0
  25. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/rustfmt.toml +0 -0
  26. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/tests/fixtures/56507.torrent.bin +0 -0
  27. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/tests/fixtures/ubuntu-22.04.2-desktop-amd64.iso.torrent.bin +0 -0
  28. {bencode_rs-0.0.2a3 → bencode_rs-0.0.3}/tests/test_1_encode.py +0 -0
@@ -116,15 +116,3 @@ jobs:
116
116
  with:
117
117
  name: wheel
118
118
  pattern: wheels-*
119
-
120
- check-dist:
121
- name: Check dist
122
- needs: [ merge-wheel-artifact ]
123
- runs-on: ubuntu-latest
124
- steps:
125
- - uses: actions/download-artifact@v4
126
- with:
127
- name: wheel
128
- path: dist
129
-
130
- - run: pipx run twine check --strict dist/*
@@ -0,0 +1,79 @@
1
+ name: ci
2
+
3
+ on:
4
+ pull_request:
5
+ branches:
6
+ - master
7
+ workflow_dispatch:
8
+ push:
9
+ branches:
10
+ - master
11
+ tags-ignore:
12
+ - "*"
13
+
14
+ jobs:
15
+ build:
16
+ concurrency:
17
+ group: "${{ github.workflow }}-${{ github.ref }}"
18
+ cancel-in-progress: true
19
+ uses: ./.github/workflows/_build_wheels.yaml
20
+
21
+ test:
22
+ strategy:
23
+ max-parallel: 5
24
+ matrix:
25
+ os: [ "ubuntu-latest", "macos-12", "macos-13", "macos-14", "windows-latest" ]
26
+ python-version:
27
+ - "3.8"
28
+ - "3.9"
29
+ - "3.10"
30
+ - "3.11"
31
+ - "3.12"
32
+ - "3.13-dev"
33
+
34
+ runs-on: "${{ matrix.os }}"
35
+
36
+ needs: [ build ]
37
+
38
+ steps:
39
+ - uses: actions/checkout@v4
40
+ - uses: actions/download-artifact@v4.1.7
41
+ with:
42
+ name: wheel
43
+ path: dist
44
+
45
+ - name: Set up Python ${{ matrix.python-version }}
46
+ uses: actions/setup-python@v5
47
+ with:
48
+ python-version: ${{ matrix.python-version }}
49
+ cache: "pip" # caching pip dependencies
50
+
51
+ - run: pip install -U pip
52
+ - run: pip install bencode-rs --no-index --find-link ./dist/
53
+ - run: pip install -r requirements.txt
54
+
55
+ - run: pytest -sv
56
+ env:
57
+ RUST_BACKTRACE: 'full'
58
+
59
+ audit:
60
+ name: Audit
61
+ needs: [ build ]
62
+ runs-on: ubuntu-latest
63
+ steps:
64
+ - uses: actions/checkout@v4
65
+
66
+ - uses: actions/setup-python@v5
67
+ with:
68
+ python-version: "3.x"
69
+ cache: "pip" # caching pip dependencies
70
+
71
+ - run: pip install -r requirements.txt
72
+
73
+ - uses: actions/download-artifact@v4
74
+ with:
75
+ name: wheel
76
+ path: dist
77
+
78
+ - run: abi3audit --verbose dist/*.whl
79
+ - run: pipx run twine check --strict dist/*
@@ -11,8 +11,11 @@ jobs:
11
11
 
12
12
  upload_all:
13
13
  name: Upload wheels to PyPI
14
- needs: [build]
14
+ needs: [ build ]
15
15
  runs-on: ubuntu-latest
16
+ environment: pypi
17
+ permissions:
18
+ id-token: write
16
19
  steps:
17
20
  - uses: actions/download-artifact@v4.1.7
18
21
  with:
@@ -20,7 +23,3 @@ jobs:
20
23
  path: dist
21
24
 
22
25
  - uses: pypa/gh-action-pypi-publish@v1.8.14
23
- with:
24
- #! Update with your project's PyPI API Token Name!
25
- user: __token__
26
- password: ${{ secrets.PYPI_API_TOKEN }}
@@ -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,26 +1,23 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: bencode-rs
3
- Version: 0.0.2a3
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
7
7
  Classifier: Programming Language :: Python :: Implementation :: CPython
8
- Requires-Dist: pre-commit ; extra == 'dev'
9
- Requires-Dist: pytest ; extra == 'dev'
10
- Requires-Dist: pytest-github-actions-annotate-failures ==0.2.0 ; extra == 'dev'
11
- Provides-Extra: dev
12
8
  License-File: LICENSE
13
9
  Summary: a bencode parser binding build with pyo3
14
10
  Keywords: bittorrent,bencode,p2p
15
11
  Requires-Python: >=3.8, <4.0
16
12
  Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
13
+ Project-URL: Homepage, https://github.com/trim21/bencode-rs
17
14
 
18
15
  # A bencode serialize/deserialize library writte in Rust
19
16
 
20
17
  ## install
21
18
 
22
19
  ```shell
23
- pip install bencode_rs
20
+ pip install bencode-rs
24
21
  ```
25
22
 
26
23
  ## basic usage
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "bencode-rs"
7
- version = "0.0.2a3"
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,14 +20,8 @@ keywords = [
20
20
  "p2p",
21
21
  ]
22
22
 
23
- [project.optional-dependencies]
24
- dev = [
25
- # lint
26
- "pre-commit",
27
- # testing
28
- "pytest",
29
- "pytest-github-actions-annotate-failures==0.2.0",
30
- ]
23
+ [project.urls]
24
+ Homepage = 'https://github.com/trim21/bencode-rs'
31
25
 
32
26
  [tool.maturin]
33
27
  features = ["pyo3/extension-module"]
@@ -3,7 +3,7 @@
3
3
  ## install
4
4
 
5
5
  ```shell
6
- pip install bencode_rs
6
+ pip install bencode-rs
7
7
  ```
8
8
 
9
9
  ## basic usage
@@ -0,0 +1,8 @@
1
+ # build
2
+ abi3audit
3
+ # lint
4
+ pre-commit
5
+ # testing
6
+ pytest
7
+ pytest-benchmark
8
+ pytest-github-actions-annotate-failures==0.2.0
@@ -1,55 +1,107 @@
1
1
  use std::borrow::Cow;
2
- use std::collections::HashMap;
2
+
3
+ use pyo3::{create_exception, PyResult, Python};
4
+ use pyo3::exceptions::PyTypeError;
3
5
  use pyo3::ffi::PyLong_FromString;
4
6
  use pyo3::prelude::*;
5
- use pyo3::{create_exception, PyResult, Python};
6
- use pyo3::types::PyDict;
7
+ use pyo3::types::{PyBytes, PyDict};
7
8
 
8
9
  create_exception!(bencode_rs,BencodeDecodeError, pyo3::exceptions::PyException);
9
10
 
10
11
  type DecodeError = BencodeDecodeError;
11
12
 
12
13
  #[pyfunction]
13
- #[pyo3(text_signature = "(b: bytes, /)")]
14
- pub fn bdecode(py: Python<'_>, b: Vec<u8>) -> PyResult<Bound<'_, PyAny>> {
15
- if b.len() == 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,
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
- fn decode_any(&mut self) -> DecodeResult {
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
 
52
- fn decode_int(&mut self) -> DecodeResult {
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
+
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,
55
107
  None => return Err(DecodeError::new_err("invalid int")),
@@ -100,12 +152,12 @@ impl<'a> Decoder<'a> {
100
152
  }
101
153
  }
102
154
 
103
- if sign > 0 {
104
- let mut val: u128 = 0;
155
+ if sign < 0 {
156
+ let mut val: i64 = 0;
105
157
 
106
158
  for c_char in self.bytes[num_start..index_e].iter() {
107
159
  let c = *c_char - b'0';
108
- val = match val.checked_mul(10).and_then(|v| v.checked_add(c as u128)) {
160
+ val = match val.checked_mul(10).and_then(|v| v.checked_add(c as i64)) {
109
161
  Some(v) => v,
110
162
  None => {
111
163
  return self.decode_int_slow(index_e);
@@ -113,80 +165,51 @@ impl<'a> Decoder<'a> {
113
165
  }
114
166
  }
115
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
+
116
176
  self.index = index_e + 1;
117
- return Ok(val.into_py(self.py));
177
+ return Ok(val.into_py(self.py).into());
178
+ }
179
+
180
+ let mut val: u64 = 0;
181
+
182
+ for c_char in self.bytes[num_start..index_e].iter() {
183
+ let c = *c_char - b'0';
184
+ val = match val.checked_mul(10).and_then(|v| v.checked_add(c as u64)) {
185
+ Some(v) => v,
186
+ None => {
187
+ return self.decode_int_slow(index_e);
188
+ }
189
+ }
118
190
  }
119
191
 
120
- return self.decode_int_slow(index_e);
192
+ self.index = index_e + 1;
193
+ return Ok(val.into_py(self.py).into());
121
194
  }
122
195
 
123
- fn decode_int_slow(&mut self, index_e: usize) -> DecodeResult {
196
+ // support int may overflow i128/u128
197
+ fn decode_int_slow(&mut self, index_e: usize) -> Result<PyObject, PyErr> {
124
198
  let s = self.bytes[self.index..index_e].to_vec();
125
199
 
126
200
  self.index = index_e + 1;
127
201
 
128
202
  let c_str = std::ffi::CString::new(s).unwrap();
203
+
129
204
  unsafe {
130
205
  let ptr = PyLong_FromString(c_str.as_ptr(), std::ptr::null_mut(), 10);
131
-
132
- // panic!("not implemented");
133
206
  return Py::from_owned_ptr_or_err(self.py, ptr);
134
207
  };
135
208
  }
136
209
 
137
- fn decode_bytes(&mut self) -> Result<Vec<u8>, PyErr> {
138
- let index_sep = match self.bytes[self.index..].iter().position(|&b| b == b':') {
139
- Some(i) => i,
140
- None => {
141
- return Err(DecodeError::new_err(format!(
142
- "invalid bytes, missing length separator: index {}",
143
- self.index
144
- )));
145
- }
146
- } + self.index;
147
-
148
- if self.bytes[self.index] == b'0' && self.index + 1 != index_sep {
149
- return Err(DecodeError::new_err(format!(
150
- "invalid bytes length, leading '0' found at index {}",
151
- self.index
152
- )));
153
- }
154
-
155
- let mut len: usize = 0;
156
- for c in self.bytes[self.index..index_sep].iter() {
157
- len = len * 10 + (c - b'0') as usize;
158
- }
159
-
160
- if index_sep + len >= self.bytes.len() {
161
- return Err(DecodeError::new_err(format!(
162
- "invalid bytes length, index out of range: index {}, len {}",
163
- self.index, len
164
- )));
165
- }
166
-
167
- let str_buff = self.bytes[index_sep + 1..index_sep + 1 + len].to_vec();
168
-
169
- self.index = index_sep + len + 1;
170
-
171
- // let o = PyBytes::new_bound(self.py, &str_buff);
172
-
173
- return Ok(str_buff);
174
- }
175
-
176
- fn check_depth(&mut self) -> Result<(), PyErr> {
177
- if self.depth > 10000 {
178
- return Err(DecodeError::new_err("object depth limit 10000 reached"));
179
- }
180
-
181
- return Ok(());
182
- }
183
-
184
- fn decode_list(&mut self) -> DecodeResult {
185
- self.depth += 1;
186
- self.check_depth()?;
187
-
210
+ fn decode_list(&mut self) -> PyResult<PyObject> {
188
211
  self.index += 1;
189
- let mut l = Vec::with_capacity(8);
212
+ let mut l = Vec::<PyObject>::with_capacity(16);
190
213
 
191
214
  loop {
192
215
  match self.bytes.get(self.index) {
@@ -200,17 +223,16 @@ impl<'a> Decoder<'a> {
200
223
  }
201
224
  }
202
225
 
203
- self.depth -= 1;
204
226
  self.index += 1;
205
- return Ok(l.into_py(self.py));
227
+
228
+
229
+ Ok(l.into_py(self.py))
206
230
  }
207
231
 
208
- fn decode_dict(&mut self) -> DecodeResult {
209
- self.depth += 1;
210
- self.check_depth()?;
232
+ fn decode_dict(&mut self) -> Result<PyObject, PyErr> {
211
233
  self.index += 1;
212
234
 
213
- let mut map: HashMap<Cow<[u8]>, Object> = HashMap::with_capacity(8);
235
+ let d = PyDict::new_bound(self.py);
214
236
  let mut last_key: Option<Cow<[u8]>> = None;
215
237
  loop {
216
238
  match self.bytes.get(self.index) {
@@ -221,28 +243,22 @@ impl<'a> Decoder<'a> {
221
243
  Some(_) => {
222
244
  let key = self.decode_bytes()?;
223
245
  let value = self.decode_any()?;
224
- let ck = Cow::from(key.clone());
225
- if !last_key.is_none() {
226
- let lk = last_key.unwrap();
246
+
247
+ let ck = Cow::from(key);
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)));
229
251
  } else if lk == ck {
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.clone());
234
- last_key = Some(ck.clone());
255
+ d.set_item(ck.clone().into_py(self.py), value.into_py(self.py))?;
256
+ // map.insert(ck.clone(), value);
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(
@@ -98,6 +98,7 @@ def test_list(raw: bytes, expected: Any):
98
98
  (b"0:", b""),
99
99
  (b"4:spam", b"spam"),
100
100
  (b"i-3e", -3),
101
+ (b"i-9223372036854775808e", -9223372036854775808), # longlong int +1
101
102
  (b"i9223372036854775808e", 9223372036854775808), # longlong int +1
102
103
  (b"i18446744073709551616e", 18446744073709551616), # unsigned long long +1
103
104
  # long long int range -9223372036854775808, 9223372036854775807
@@ -1,35 +0,0 @@
1
- name: Build Wheels
2
-
3
- on:
4
- push:
5
- branches:
6
- - master
7
- tags-ignore:
8
- - "*"
9
-
10
- jobs:
11
- build:
12
- concurrency:
13
- group: "${{ github.workflow }}-${{ github.ref }}"
14
- cancel-in-progress: true
15
- uses: ./.github/workflows/_build_wheels.yaml
16
-
17
- audit:
18
- name: Audit
19
- needs: [build]
20
- runs-on: ubuntu-latest
21
- steps:
22
- - uses: actions/checkout@v4
23
-
24
- - uses: actions/setup-python@v5
25
- with:
26
- python-version: "3.x"
27
-
28
- - run: pip install abi3audit
29
-
30
- - uses: actions/download-artifact@v4
31
- with:
32
- name: wheel
33
- path: dist
34
-
35
- - run: abi3audit --verbose dist/*.whl
@@ -1,45 +0,0 @@
1
- name: Tests
2
-
3
- on:
4
- pull_request:
5
- branches:
6
- - master
7
- workflow_dispatch:
8
- push:
9
- branches:
10
- - master
11
- tags-ignore:
12
- - "*"
13
-
14
- jobs:
15
- test:
16
- strategy:
17
- max-parallel: 5
18
- matrix:
19
- os: ["ubuntu-latest", "macos-latest", "windows-latest"]
20
- python-version:
21
- - "3.8"
22
- - "3.9"
23
- - "3.10"
24
- - "3.11"
25
- - "3.12"
26
-
27
- runs-on: "${{ matrix.os }}"
28
-
29
- steps:
30
- - uses: actions/checkout@v4
31
- - uses: actions-rust-lang/setup-rust-toolchain@v1
32
-
33
- - name: Set up Python ${{ matrix.python-version }}
34
- uses: actions/setup-python@v5
35
- with:
36
- python-version: ${{ matrix.python-version }}
37
- cache: "pip" # caching pip dependencies
38
-
39
- - run: pip install -U pip
40
- - run: pip install -e '.[dev]'
41
-
42
- - name: Run pytest
43
- run: python -m pytest -sv
44
- env:
45
- RUST_BACKTRACE: 'full'
@@ -1,53 +0,0 @@
1
- import pathlib
2
- import sys
3
-
4
- import pytest
5
- import bencodepy
6
-
7
- from bencode2 import bencode
8
- from bencode2 import bdecode
9
-
10
- root = pathlib.Path(__file__).parent
11
-
12
- torrent_1_raw = root.joinpath("tests/fixtures/56507.torrent.bin").read_bytes()
13
- torrent_1_data = bencodepy.bdecode(torrent_1_raw)
14
-
15
- torrent_2_raw = root.joinpath(
16
- "tests/fixtures/ubuntu-22.04.2-desktop-amd64.iso.torrent.bin"
17
- ).read_bytes()
18
- torrent_2_data = bencodepy.bdecode(torrent_2_raw)
19
-
20
- os = sys.platform
21
- py_ver = "{}{}".format(*sys.version_info[:2])
22
-
23
-
24
- def mark(fn):
25
- return pytest.mark.parametrize(["os", "ver"], [(os, py_ver)])(fn)
26
-
27
-
28
- def decode(b: bytes):
29
- return bdecode(b)
30
-
31
-
32
- def encode(data):
33
- return bencode(data)
34
-
35
-
36
- @mark
37
- def test_decode_1(benchmark, os, ver):
38
- benchmark(decode, torrent_1_raw)
39
-
40
-
41
- @mark
42
- def test_decode_2(benchmark, os, ver):
43
- benchmark(decode, torrent_2_raw)
44
-
45
-
46
- @mark
47
- def test_encode_1(benchmark, os, ver):
48
- benchmark(encode, torrent_1_data)
49
-
50
-
51
- @mark
52
- def test_encode_2(benchmark, os, ver):
53
- benchmark(encode, torrent_2_data)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes