Socket
Socket
Sign inDemoInstall

bencode2

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bencode2

A fast and correct bencode serialize/deserialize library


Maintainers
1

A fast and correct bencode serialize/deserialize library

PyPI tests PyPI - Python Version Codecov branch

introduction

Why yet another bencode package in python?

because I need a bencode library:

  1. Correct, which mean it should fully validate its inputs, and won't try to decode bencode bytes to str by default. Bencode doesn't have a utf-8 str type, only bytes, so many decoder try to decode bytes to str and fallback to bytes, this package won't, it parse bencode bytes value as python bytes.
  2. Fast enough, that's why this package is written with c++.
  3. even cross implement, what's why this package sill have a pure python fallback and bencode2-${version}-py3-none-any.whl wheel on pypi.

install

pip install bencode2

basic usage

import bencode2

assert bencode2.bdecode(b"d4:spaml1:a1:bee") == {b"spam": [b"a", b"b"]}

assert bencode2.bencode({'hello': 'world'}) == b'd5:hello5:worlde'

Decoding

bencode typepython type
integerint
stringbytes
arraylist
directorydict

bencode have 4 native types, integer, string, array and directory.

This package will decode integer to int, array to list and directory to dict.

Because bencode string is not defined as utf-8 string, and will contain raw bytes bencode2 will decode bencode string to python bytes.

Encoding

python typebencode type
boolinteger 0/1
int, enum.IntEnuminteger
str, enum.StrEnumstring
bytes, bytearray,memoryviewstring
list, tuple, NamedTuplearray
dict, OrderedDictdirectory
types.MaapingProxydirectory
dataclassesdirectory

free threading

bencode2 have a free threading wheel on pypi, build with GIL disabled.

When encoding or decoding, it will not acquire GIL, which mean it's the caller's responsibility to ensure thread safety.

When calling bencode, it's safe to encode same object in multiple threading, but it's not safe to encoding a object and change it in another thread at same time.

Also, when decoding, bytes objects are immutable so it's safe to be used in multiple threading, but memoryview and bytearray maybe not, please make sure underlay data doesn't change when decoding.

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc