File: flatbencode.py

package info (click to toggle)
torf 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 988 kB
  • sloc: python: 10,054; makefile: 15; sh: 8
file content (23 lines) | stat: -rw-r--r-- 782 bytes parent folder | download
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']