1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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']
|