bencode-rs 0.0.2a3__tar.gz → 0.0.2a4__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.2a4}/.github/workflows/_build_wheels.yaml +0 -12
  2. bencode_rs-0.0.2a4/.github/workflows/ci.yaml +79 -0
  3. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/.github/workflows/release.yaml +4 -5
  4. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/PKG-INFO +2 -6
  5. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/pyproject.toml +1 -10
  6. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/readme.md +1 -1
  7. bencode_rs-0.0.2a4/requirements.txt +8 -0
  8. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/src/decode.rs +39 -39
  9. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/tests/test_2_decode.py +1 -0
  10. bencode_rs-0.0.2a3/.github/workflows/build.yaml +0 -35
  11. bencode_rs-0.0.2a3/.github/workflows/tests.yaml +0 -45
  12. bencode_rs-0.0.2a3/bench.py +0 -53
  13. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/.coveragerc +0 -0
  14. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/.gitattributes +0 -0
  15. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/.github/renovate.json +0 -0
  16. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/.gitignore +0 -0
  17. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/Cargo.lock +0 -0
  18. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/Cargo.toml +0 -0
  19. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/LICENSE +0 -0
  20. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/bencode_rs/__init__.py +0 -0
  21. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/bencode_rs/__init__.pyi +0 -0
  22. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/leak.py +0 -0
  23. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/rustfmt.toml +0 -0
  24. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/src/encode.rs +0 -0
  25. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/src/lib.rs +0 -0
  26. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/tests/fixtures/56507.torrent.bin +0 -0
  27. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/tests/fixtures/ubuntu-22.04.2-desktop-amd64.iso.torrent.bin +0 -0
  28. {bencode_rs-0.0.2a3 → bencode_rs-0.0.2a4}/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 }}
@@ -1,14 +1,10 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: bencode-rs
3
- Version: 0.0.2a3
3
+ Version: 0.0.2a4
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
@@ -20,7 +16,7 @@ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
20
16
  ## install
21
17
 
22
18
  ```shell
23
- pip install bencode_rs
19
+ pip install bencode-rs
24
20
  ```
25
21
 
26
22
  ## 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.2a4"
8
8
  requires-python = ">=3.8,<4.0"
9
9
  description = "a bencode parser binding build with pyo3"
10
10
  readme = "readme.md"
@@ -20,15 +20,6 @@ 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
- ]
31
-
32
23
  [tool.maturin]
33
24
  features = ["pyo3/extension-module"]
34
25
  module-name = "bencode_rs._bencode"
@@ -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,23 +1,23 @@
1
1
  use std::borrow::Cow;
2
2
  use std::collections::HashMap;
3
- use pyo3::ffi::PyLong_FromString;
3
+ use pyo3::ffi::{PyLong_FromString};
4
4
  use pyo3::prelude::*;
5
5
  use pyo3::{create_exception, PyResult, Python};
6
- use pyo3::types::PyDict;
6
+ use pyo3::exceptions::PyOverflowError;
7
+ use pyo3::types::{PyBytes, PyDict, PyLong};
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 {
14
+ pub fn bdecode<'py>(py: Python<'py>, b: Bound<'py, PyBytes>) -> PyResult<Bound<'py, PyAny>> {
15
+ if b.len().unwrap() == 0 {
16
16
  return Err(DecodeError::new_err("empty byte sequence"));
17
17
  }
18
18
 
19
19
  let mut ctx = Decoder {
20
- bytes: b,
20
+ bytes: b.as_bytes().to_vec(),
21
21
  index: 0,
22
22
  py,
23
23
  depth: 0,
@@ -39,7 +39,7 @@ struct Decoder<'a> {
39
39
  }
40
40
 
41
41
  impl<'a> Decoder<'a> {
42
- fn decode_any(&mut self) -> DecodeResult {
42
+ fn decode_any(&mut self) -> Result<PyObject, PyErr> {
43
43
  return match self.current_byte()? {
44
44
  b'i' => self.decode_int(),
45
45
  b'0'..=b'9' => Ok(Cow::from(self.decode_bytes()?).into_py(self.py)),
@@ -49,7 +49,7 @@ impl<'a> Decoder<'a> {
49
49
  };
50
50
  }
51
51
 
52
- fn decode_int(&mut self) -> DecodeResult {
52
+ fn decode_int(&mut self) -> Result<PyObject, PyErr> {
53
53
  let index_e = match self.bytes[self.index..].iter().position(|&b| b == b'e') {
54
54
  Some(i) => i,
55
55
  None => return Err(DecodeError::new_err("invalid int")),
@@ -100,27 +100,28 @@ impl<'a> Decoder<'a> {
100
100
  }
101
101
  }
102
102
 
103
- if sign > 0 {
104
- let mut val: u128 = 0;
103
+ if sign < 0 {
104
+ // slow path to build PyLong with python
105
+ return self.decode_int_slow(index_e);
106
+ }
105
107
 
106
- for c_char in self.bytes[num_start..index_e].iter() {
107
- let c = *c_char - b'0';
108
- val = match val.checked_mul(10).and_then(|v| v.checked_add(c as u128)) {
109
- Some(v) => v,
110
- None => {
111
- return self.decode_int_slow(index_e);
112
- }
108
+ let mut val: u128 = 0;
109
+
110
+ for c_char in self.bytes[num_start..index_e].iter() {
111
+ let c = *c_char - b'0';
112
+ val = match val.checked_mul(10).and_then(|v| v.checked_add(c as u128)) {
113
+ Some(v) => v,
114
+ None => {
115
+ return self.decode_int_slow(index_e);
113
116
  }
114
117
  }
115
-
116
- self.index = index_e + 1;
117
- return Ok(val.into_py(self.py));
118
118
  }
119
119
 
120
- return self.decode_int_slow(index_e);
120
+ self.index = index_e + 1;
121
+ return Ok(val.into_py(self.py).into());
121
122
  }
122
123
 
123
- fn decode_int_slow(&mut self, index_e: usize) -> DecodeResult {
124
+ fn decode_int_slow(&mut self, index_e: usize) -> Result<PyObject, PyErr> {
124
125
  let s = self.bytes[self.index..index_e].to_vec();
125
126
 
126
127
  self.index = index_e + 1;
@@ -173,17 +174,17 @@ impl<'a> Decoder<'a> {
173
174
  return Ok(str_buff);
174
175
  }
175
176
 
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
- }
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
+ // }
183
184
 
184
185
  fn decode_list(&mut self) -> DecodeResult {
185
- self.depth += 1;
186
- self.check_depth()?;
186
+ // self.depth += 1;
187
+ // self.check_depth()?;
187
188
 
188
189
  self.index += 1;
189
190
  let mut l = Vec::with_capacity(8);
@@ -200,14 +201,14 @@ impl<'a> Decoder<'a> {
200
201
  }
201
202
  }
202
203
 
203
- self.depth -= 1;
204
+ // self.depth -= 1;
204
205
  self.index += 1;
205
206
  return Ok(l.into_py(self.py));
206
207
  }
207
208
 
208
209
  fn decode_dict(&mut self) -> DecodeResult {
209
- self.depth += 1;
210
- self.check_depth()?;
210
+ // self.depth += 1;
211
+ // self.check_depth()?;
211
212
  self.index += 1;
212
213
 
213
214
  let mut map: HashMap<Cow<[u8]>, Object> = HashMap::with_capacity(8);
@@ -222,16 +223,15 @@ impl<'a> Decoder<'a> {
222
223
  let key = self.decode_bytes()?;
223
224
  let value = self.decode_any()?;
224
225
  let ck = Cow::from(key.clone());
225
- if !last_key.is_none() {
226
- let lk = last_key.unwrap();
226
+ if let Some(lk) = last_key {
227
227
  if lk > ck {
228
228
  return Err(DecodeError::new_err(format!("dict key not sorted. index {}", self.index)));
229
229
  } else if lk == ck {
230
230
  return Err(DecodeError::new_err(format!("duplicated dict key found: index {}", self.index)));
231
231
  }
232
232
  }
233
- map.insert(ck.clone(), value.clone());
234
- last_key = Some(ck.clone());
233
+ map.insert(ck.clone(), value);
234
+ last_key = Some(ck);
235
235
  }
236
236
  }
237
237
  }
@@ -242,7 +242,7 @@ impl<'a> Decoder<'a> {
242
242
  d.set_item(k.to_owned().into_py(self.py), v.to_owned())?;
243
243
  }
244
244
 
245
- self.depth -= 1;
245
+ // self.depth -= 1;
246
246
  self.index += 1;
247
247
  return Ok(d.into_py(self.py));
248
248
  }
@@ -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
File without changes
File without changes
File without changes