1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
From: Nadzeya Hutsko <nadzya.info@gmail.com>
Date: Mon, 12 Jan 2026 21:35:38 +0100
Subject: Add flatbencode compatibility shim for fastbencode
flatbencode is unmaintained (last update 2016) and not packaged in Debian.
This shim provides the flatbencode API using the maintained fastbencode
library that is already available in Debian as python3-fastbencode.
The API mapping:
- flatbencode.encode() -> fastbencode.bencode()
- flatbencode.decode() -> fastbencode.bdecode()
- Handle bytearray conversion for compatibility
Forwarded: https://github.com/rndusr/torf/issues/61
---
pyproject.toml | 2 +-
tests/test_read.py | 2 +-
tests/test_write.py | 2 +-
torf/_torrent.py | 2 +-
torf/flatbencode.py | 23 +++++++++++++++++++++++
5 files changed, 27 insertions(+), 4 deletions(-)
create mode 100644 torf/flatbencode.py
diff --git a/pyproject.toml b/pyproject.toml
index 138f1a8..c616e46 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -23,7 +23,7 @@ classifiers = [
]
requires-python = ">=3.8"
dependencies = [
- "flatbencode==0.2.*",
+ "fastbencode>=0.3",
]
[project.optional-dependencies]
diff --git a/tests/test_read.py b/tests/test_read.py
index e82c793..761d4e4 100644
--- a/tests/test_read.py
+++ b/tests/test_read.py
@@ -4,7 +4,7 @@ from datetime import datetime
from hashlib import sha1
from pathlib import Path
-import flatbencode as bencode
+import torf.flatbencode as bencode
import pytest
import torf
diff --git a/tests/test_write.py b/tests/test_write.py
index f32a6eb..234e563 100644
--- a/tests/test_write.py
+++ b/tests/test_write.py
@@ -1,7 +1,7 @@
import os
import time
-import flatbencode as bencode
+import torf.flatbencode as bencode
import pytest
import torf
diff --git a/torf/_torrent.py b/torf/_torrent.py
index 1dedb5c..b8e769e 100644
--- a/torf/_torrent.py
+++ b/torf/_torrent.py
@@ -25,7 +25,7 @@ import re
from collections import abc
from datetime import datetime
-import flatbencode as bencode
+from . import flatbencode as bencode
from . import __version__
from . import _errors as error
diff --git a/torf/flatbencode.py b/torf/flatbencode.py
new file mode 100644
index 0000000..aea9229
--- /dev/null
+++ b/torf/flatbencode.py
@@ -0,0 +1,23 @@
+# Compatibility shim: flatbencode API using fastbencode backend
+# This allows torf to use the maintained fastbencode library
+# while keeping the original flatbencode API.
+
+from fastbencode import bencode as _bencode, bdecode as _bdecode
+
+# flatbencode accepts both bytes and bytearray for decode
+# fastbencode only accepts bytes, so we need to convert
+def decode(s):
+ """Decode bencode data, accepting both bytes and bytearray."""
+ if isinstance(s, bytearray):
+ s = bytes(s)
+ return _bdecode(s)
+
+def encode(obj):
+ """Encode Python object to bencode format."""
+ return _bencode(obj)
+
+# flatbencode raises DecodingError (subclass of ValueError)
+# fastbencode raises ValueError directly
+DecodingError = ValueError
+
+__all__ = ['encode', 'decode', 'DecodingError']
|