././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1773749457.4158168 fastbencode-0.3.10/0000755000175100017510000000000015156242321013517 5ustar00runnerrunner././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/COPYING0000644000175100017510000002613615156242311014561 0ustar00runnerrunner Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/Cargo.toml0000644000175100017510000000067215156242311015453 0ustar00runnerrunner[package] name = "fastbencode" version = "0.3.10" edition = "2021" authors = ["Jelmer Vernooij "] license = "Apache-2.0" description = "Implementation of bencode with Rust implementation" readme = "README.md" repository = "https://github.com/breezy-team/fastbencode" publish = false [lib] name = "fastbencode__bencode_rs" crate-type = ["cdylib"] [dependencies] pyo3 = { version = "0.28", features = ["extension-module"] } ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/MANIFEST.in0000644000175100017510000000021215156242311015247 0ustar00runnerrunnerinclude README.md include COPYING include fastbencode/py.typed include Cargo.toml recursive-include tests *.py recursive-include src *.rs ././@PaxHeader0000000000000000000000000000003300000000000010211 xustar0027 mtime=1773749457.415789 fastbencode-0.3.10/PKG-INFO0000644000175100017510000000410715156242321014616 0ustar00runnerrunnerMetadata-Version: 2.4 Name: fastbencode Version: 0.3.10 Summary: Implementation of bencode with optional fast Rust extensions Maintainer-email: Breezy Developers License-Expression: Apache-2.0 Project-URL: Homepage, https://github.com/breezy-team/fastbencode Project-URL: GitHub, https://github.com/breezy-team/fastbencode Classifier: Programming Language :: Python :: 3.10 Classifier: Programming Language :: Python :: 3.11 Classifier: Programming Language :: Python :: 3.12 Classifier: Programming Language :: Python :: 3.13 Classifier: Programming Language :: Python :: 3.14 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Operating System :: POSIX Classifier: Operating System :: Microsoft :: Windows Requires-Python: >=3.10 Description-Content-Type: text/markdown License-File: COPYING Provides-Extra: rust Requires-Dist: setuptools-rust>=1.0.0; extra == "rust" Provides-Extra: dev Requires-Dist: ruff==0.15.6; extra == "dev" Dynamic: license-file fastbencode =========== fastbencode is an implementation of the bencode serialization format originally used by BitTorrent. The package includes both a pure-Python version and an optional Rust extension based on PyO3. Both provide the same functionality, but the Rust extension provides significantly better performance. Example: >>> from fastbencode import bencode, bdecode >>> bencode([1, 2, b'a', {b'd': 3}]) b'li1ei2e1:ad1:di3eee' >>> bdecode(bencode([1, 2, b'a', {b'd': 3}])) [1, 2, b'a', {b'd': 3}] The default ``bencode``/``bdecode`` functions just operate on bytestrings. Use ``bencode_utf8`` / ``bdecode_utf8`` to serialize/deserialize all plain strings as UTF-8 bytestrings. Note that for performance reasons, all dictionary keys still have to be bytestrings. License ======= fastbencode is available under the Apache License, version 2. Copyright ========= * Original Pure-Python bencoder © Petru Paler * Split out from Bazaar/Breezy by Jelmer Vernooij * Rust extension © Jelmer Vernooij ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/README.md0000644000175100017510000000200515156242311014772 0ustar00runnerrunnerfastbencode =========== fastbencode is an implementation of the bencode serialization format originally used by BitTorrent. The package includes both a pure-Python version and an optional Rust extension based on PyO3. Both provide the same functionality, but the Rust extension provides significantly better performance. Example: >>> from fastbencode import bencode, bdecode >>> bencode([1, 2, b'a', {b'd': 3}]) b'li1ei2e1:ad1:di3eee' >>> bdecode(bencode([1, 2, b'a', {b'd': 3}])) [1, 2, b'a', {b'd': 3}] The default ``bencode``/``bdecode`` functions just operate on bytestrings. Use ``bencode_utf8`` / ``bdecode_utf8`` to serialize/deserialize all plain strings as UTF-8 bytestrings. Note that for performance reasons, all dictionary keys still have to be bytestrings. License ======= fastbencode is available under the Apache License, version 2. Copyright ========= * Original Pure-Python bencoder © Petru Paler * Split out from Bazaar/Breezy by Jelmer Vernooij * Rust extension © Jelmer Vernooij ././@PaxHeader0000000000000000000000000000003300000000000010211 xustar0027 mtime=1773749457.413841 fastbencode-0.3.10/fastbencode/0000755000175100017510000000000015156242321015774 5ustar00runnerrunner././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/fastbencode/__init__.py0000644000175100017510000000262415156242311020110 0ustar00runnerrunner# Copyright (C) 2021-2023 Jelmer Vernooij # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA """Wrapper around the bencode Rust and Python implementations.""" from typing import Type __version__ = (0, 3, 10) Bencached: Type try: from fastbencode._bencode_rs import ( Bencached, bdecode, bdecode_as_tuple, bdecode_utf8, bencode, bencode_utf8, ) except ModuleNotFoundError as e: import warnings warnings.warn(f"failed to load compiled extension: {e}", UserWarning) # Fall back to pure Python implementation from ._bencode_py import ( # noqa: F401 Bencached, bdecode, bdecode_as_tuple, bdecode_utf8, bencode, bencode_utf8, ) ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/fastbencode/_bencode_py.py0000644000175100017510000001261415156242311020617 0ustar00runnerrunner# bencode structured encoding # # Written by Petru Paler # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation files # (the "Software"), to deal in the Software without restriction, # including without limitation the rights to use, copy, modify, merge, # publish, distribute, sublicense, and/or sell copies of the Software, # and to permit persons to whom the Software is furnished to do so, # subject to the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # Modifications copyright (C) 2008 Canonical Ltd # Modifications copyright (C) 2021-2023 Jelmer Vernooij from typing import Callable, Dict, List, Type class BDecoder: def __init__(self, yield_tuples=False, bytestring_encoding=None) -> None: """Constructor. :param yield_tuples: if true, decode "l" elements as tuples rather than lists. """ self.yield_tuples = yield_tuples self.bytestring_encoding = bytestring_encoding decode_func = {} decode_func[b"l"] = self.decode_list decode_func[b"d"] = self.decode_dict decode_func[b"i"] = self.decode_int decode_func[b"0"] = self.decode_bytes decode_func[b"1"] = self.decode_bytes decode_func[b"2"] = self.decode_bytes decode_func[b"3"] = self.decode_bytes decode_func[b"4"] = self.decode_bytes decode_func[b"5"] = self.decode_bytes decode_func[b"6"] = self.decode_bytes decode_func[b"7"] = self.decode_bytes decode_func[b"8"] = self.decode_bytes decode_func[b"9"] = self.decode_bytes self.decode_func = decode_func def decode_int(self, x, f): f += 1 newf = x.index(b"e", f) n = int(x[f:newf]) if x[f : f + 2] == b"-0": raise ValueError elif x[f : f + 1] == b"0" and newf != f + 1: raise ValueError return (n, newf + 1) def decode_bytes(self, x, f): colon = x.index(b":", f) n = int(x[f:colon]) if x[f : f + 1] == b"0" and colon != f + 1: raise ValueError colon += 1 d = x[colon : colon + n] if self.bytestring_encoding: d = d.decode(self.bytestring_encoding) return (d, colon + n) def decode_list(self, x, f): r, f = [], f + 1 while x[f : f + 1] != b"e": v, f = self.decode_func[x[f : f + 1]](x, f) r.append(v) if self.yield_tuples: r = tuple(r) return (r, f + 1) def decode_dict(self, x, f): r, f = {}, f + 1 lastkey = None while x[f : f + 1] != b"e": k, f = self.decode_bytes(x, f) if lastkey is not None and lastkey >= k: raise ValueError lastkey = k r[k], f = self.decode_func[x[f : f + 1]](x, f) return (r, f + 1) def bdecode(self, x): if not isinstance(x, bytes): raise TypeError try: r, l = self.decode_func[x[:1]](x, 0) # noqa: E741 except (IndexError, KeyError, OverflowError) as e: raise ValueError(str(e)) if l != len(x): # noqa: E741 raise ValueError return r _decoder = BDecoder() bdecode = _decoder.bdecode _tuple_decoder = BDecoder(True) bdecode_as_tuple = _tuple_decoder.bdecode _utf8_decoder = BDecoder(bytestring_encoding="utf-8") bdecode_utf8 = _utf8_decoder.bdecode class Bencached: __slots__ = ["bencoded"] def __init__(self, s) -> None: self.bencoded = s class BEncoder: def __init__(self, bytestring_encoding=None): self.bytestring_encoding = bytestring_encoding self.encode_func: Dict[Type, Callable[[object, List[bytes]], None]] = { Bencached: self.encode_bencached, int: self.encode_int, bytes: self.encode_bytes, list: self.encode_list, tuple: self.encode_list, dict: self.encode_dict, bool: self.encode_bool, str: self.encode_str, } def encode_bencached(self, x, r): r.append(x.bencoded) def encode_bool(self, x, r): self.encode_int(int(x), r) def encode_int(self, x, r): r.extend((b"i", int_to_bytes(x), b"e")) def encode_bytes(self, x, r): r.extend((int_to_bytes(len(x)), b":", x)) def encode_list(self, x, r): r.append(b"l") for i in x: self.encode(i, r) r.append(b"e") def encode_dict(self, x, r): r.append(b"d") ilist = sorted(x.items()) for k, v in ilist: r.extend((int_to_bytes(len(k)), b":", k)) self.encode(v, r) r.append(b"e") def encode_str(self, x, r): if self.bytestring_encoding is None: raise TypeError( "string found but no encoding specified. " "Use bencode_utf8 rather bencode?" ) return self.encode_bytes(x.encode(self.bytestring_encoding), r) def encode(self, x, r): self.encode_func[type(x)](x, r) def int_to_bytes(n): return b"%d" % n def bencode(x): r = [] encoder = BEncoder() encoder.encode(x, r) return b"".join(r) def bencode_utf8(x): r = [] encoder = BEncoder(bytestring_encoding="utf-8") encoder.encode(x, r) return b"".join(r) ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/fastbencode/py.typed0000644000175100017510000000000015156242311017460 0ustar00runnerrunner././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1773749457.4152234 fastbencode-0.3.10/fastbencode.egg-info/0000755000175100017510000000000015156242321017466 5ustar00runnerrunner././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749457.0 fastbencode-0.3.10/fastbencode.egg-info/PKG-INFO0000644000175100017510000000410715156242321020565 0ustar00runnerrunnerMetadata-Version: 2.4 Name: fastbencode Version: 0.3.10 Summary: Implementation of bencode with optional fast Rust extensions Maintainer-email: Breezy Developers License-Expression: Apache-2.0 Project-URL: Homepage, https://github.com/breezy-team/fastbencode Project-URL: GitHub, https://github.com/breezy-team/fastbencode Classifier: Programming Language :: Python :: 3.10 Classifier: Programming Language :: Python :: 3.11 Classifier: Programming Language :: Python :: 3.12 Classifier: Programming Language :: Python :: 3.13 Classifier: Programming Language :: Python :: 3.14 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Operating System :: POSIX Classifier: Operating System :: Microsoft :: Windows Requires-Python: >=3.10 Description-Content-Type: text/markdown License-File: COPYING Provides-Extra: rust Requires-Dist: setuptools-rust>=1.0.0; extra == "rust" Provides-Extra: dev Requires-Dist: ruff==0.15.6; extra == "dev" Dynamic: license-file fastbencode =========== fastbencode is an implementation of the bencode serialization format originally used by BitTorrent. The package includes both a pure-Python version and an optional Rust extension based on PyO3. Both provide the same functionality, but the Rust extension provides significantly better performance. Example: >>> from fastbencode import bencode, bdecode >>> bencode([1, 2, b'a', {b'd': 3}]) b'li1ei2e1:ad1:di3eee' >>> bdecode(bencode([1, 2, b'a', {b'd': 3}])) [1, 2, b'a', {b'd': 3}] The default ``bencode``/``bdecode`` functions just operate on bytestrings. Use ``bencode_utf8`` / ``bdecode_utf8`` to serialize/deserialize all plain strings as UTF-8 bytestrings. Note that for performance reasons, all dictionary keys still have to be bytestrings. License ======= fastbencode is available under the Apache License, version 2. Copyright ========= * Original Pure-Python bencoder © Petru Paler * Split out from Bazaar/Breezy by Jelmer Vernooij * Rust extension © Jelmer Vernooij ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749457.0 fastbencode-0.3.10/fastbencode.egg-info/SOURCES.txt0000644000175100017510000000055115156242321021353 0ustar00runnerrunnerCOPYING Cargo.toml MANIFEST.in README.md pyproject.toml setup.py fastbencode/__init__.py fastbencode/_bencode_py.py fastbencode/py.typed fastbencode.egg-info/PKG-INFO fastbencode.egg-info/SOURCES.txt fastbencode.egg-info/dependency_links.txt fastbencode.egg-info/requires.txt fastbencode.egg-info/top_level.txt src/lib.rs tests/__init__.py tests/test_bencode.py././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749457.0 fastbencode-0.3.10/fastbencode.egg-info/dependency_links.txt0000644000175100017510000000000115156242321023534 0ustar00runnerrunner ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749457.0 fastbencode-0.3.10/fastbencode.egg-info/requires.txt0000644000175100017510000000006315156242321022065 0ustar00runnerrunner [dev] ruff==0.15.6 [rust] setuptools-rust>=1.0.0 ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749457.0 fastbencode-0.3.10/fastbencode.egg-info/top_level.txt0000644000175100017510000000001415156242321022213 0ustar00runnerrunnerfastbencode ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/pyproject.toml0000644000175100017510000000472215156242311016437 0ustar00runnerrunner[build-system] requires = [ "setuptools>=61.2", "setuptools-rust>=1.0.0", ] build-backend = "setuptools.build_meta" [project] name = "fastbencode" description = "Implementation of bencode with optional fast Rust extensions" maintainers = [{name = "Breezy Developers", email = "breezy-core@googlegroups.com"}] readme = "README.md" license = "Apache-2.0" classifiers = [ "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", "Programming Language :: Python :: 3.14", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", "Operating System :: POSIX", "Operating System :: Microsoft :: Windows", ] requires-python = ">=3.10" dynamic = ["version"] dependencies = [] [project.urls] Homepage = "https://github.com/breezy-team/fastbencode" GitHub = "https://github.com/breezy-team/fastbencode" [project.optional-dependencies] rust = ["setuptools-rust>=1.0.0"] dev = [ "ruff==0.15.6" ] [tool.setuptools] packages = ["fastbencode"] include-package-data = false [tool.setuptools.dynamic] version = {attr = "fastbencode.__version__"} [tool.ruff] target-version = "py37" line-length = 79 [tool.ruff.lint] select = [ "ANN", "D", "E", "F", "I", "UP", ] ignore = [ "ANN001", "ANN002", "ANN201", "ANN202", "ANN204", "D100", "D101", "D102", "D103", "D105", "D107", ] [tool.ruff.lint.pydocstyle] convention = "google" [tool.cibuildwheel] environment = {PATH="$HOME/.cargo/bin:$PATH"} before-build = "pip install -U setuptools-rust && curl https://sh.rustup.rs -sSf | sh -s -- --profile=minimal -y && rustup show" [tool.cibuildwheel.linux] skip = "*-musllinux_* *cp314*" archs = ["auto", "aarch64"] before-build = "pip install -U setuptools-rust && yum -y install libatomic && curl https://sh.rustup.rs -sSf | sh -s -- --profile=minimal -y && rustup show" [tool.cibuildwheel.macos] archs = ["auto", "universal2", "x86_64", "arm64"] before-all = "rustup target add x86_64-apple-darwin aarch64-apple-darwin" skip = """\ cp39-macosx_x86_64 cp39-macosx_universal2 \ cp310-macosx_x86_64 cp310-macosx_universal2 \ cp311-macosx_x86_64 cp311-macosx_universal2 \ cp312-macosx_x86_64 cp312-macosx_universal2 \ cp313-macosx_x86_64 cp313-macosx_universal2 \ cp314-macosx_x86_64 cp314-macosx_universal2 \ """ ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1773749457.4160988 fastbencode-0.3.10/setup.cfg0000644000175100017510000000004615156242321015340 0ustar00runnerrunner[egg_info] tag_build = tag_date = 0 ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/setup.py0000755000175100017510000000046315156242311015236 0ustar00runnerrunner#!/usr/bin/python3 from setuptools import setup from setuptools_rust import Binding, RustExtension setup( rust_extensions=[ RustExtension( "fastbencode._bencode_rs", binding=Binding.PyO3, py_limited_api=False, optional=True, ) ], ) ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1773749457.4147387 fastbencode-0.3.10/src/0000755000175100017510000000000015156242321014306 5ustar00runnerrunner././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/src/lib.rs0000644000175100017510000003355715156242311015436 0ustar00runnerrunner#![allow(non_snake_case)] use pyo3::exceptions::{PyTypeError, PyValueError}; use pyo3::prelude::*; use pyo3::types::{PyBytes, PyDict, PyInt, PyList, PyString, PyTuple}; #[pyclass] struct Bencached { #[pyo3(get)] bencoded: Py, } #[pymethods] impl Bencached { #[new] fn new(s: Py) -> Self { Bencached { bencoded: s } } fn as_bytes(&self, py: Python) -> PyResult<&[u8]> { Ok(self.bencoded.as_bytes(py)) } } #[pyclass] struct Decoder { data: Vec, position: usize, yield_tuples: bool, bytestring_encoding: Option, } #[pymethods] impl Decoder { #[new] fn new( s: &Bound, yield_tuples: Option, bytestring_encoding: Option, ) -> Self { Decoder { data: s.as_bytes().to_vec(), position: 0, yield_tuples: yield_tuples.unwrap_or(false), bytestring_encoding, } } fn decode<'py>(&mut self, py: Python<'py>) -> PyResult> { let result = self.decode_object(py)?; if self.position < self.data.len() { return Err(PyValueError::new_err("junk in stream")); } Ok(result) } fn decode_object<'py>(&mut self, py: Python<'py>) -> PyResult> { if self.position >= self.data.len() { return Err(PyValueError::new_err("stream underflow")); } // Check for recursion - in a real implementation we would track recursion depth let next_byte = self.data[self.position]; match next_byte { b'0'..=b'9' => Ok(self.decode_bytes(py)?.into_any()), b'l' => { self.position += 1; Ok(self.decode_list(py)?.into_any()) } b'i' => { self.position += 1; Ok(self.decode_int(py)?.into_any()) } b'd' => { self.position += 1; Ok(self.decode_dict(py)?.into_any()) } _ => Err(PyValueError::new_err(format!( "unknown object type identifier {:?}", next_byte as char ))), } } fn read_digits(&mut self, stop_char: u8) -> PyResult { let start = self.position; while self.position < self.data.len() { let b = self.data[self.position]; if b == stop_char { break; } if (b < b'0' || b > b'9') && b != b'-' { return Err(PyValueError::new_err(format!( "Stop character {} not found: {}", stop_char as char, b as char ))); } self.position += 1; } if self.position >= self.data.len() || self.data[self.position] != stop_char { return Err(PyValueError::new_err(format!( "Stop character {} not found", stop_char as char ))); } // Check for leading zeros if self.data[start] == b'0' && self.position - start > 1 { return Err(PyValueError::new_err("leading zeros are not allowed")); } else if self.data[start] == b'-' && self.data[start + 1] == b'0' && self.position - start > 2 { return Err(PyValueError::new_err("leading zeros are not allowed")); } Ok(String::from_utf8_lossy(&self.data[start..self.position]).to_string()) } fn decode_int<'py>(&mut self, py: Python<'py>) -> PyResult> { let digits = self.read_digits(b'e')?; // Move past the 'e' self.position += 1; // Check for negative zero if digits == "-0" { return Err(PyValueError::new_err("negative zero not allowed")); } // Parse the integer directly let parsed_int = match digits.parse::() { Ok(n) => n.into_pyobject(py)?.into_any(), Err(_) => { // For very large integers, fallback to Python's conversion let py_str = PyString::new(py, &digits); let int_type = py.get_type::(); int_type.call1((py_str,))? } }; Ok(parsed_int.into_any()) } fn decode_bytes<'py>(&mut self, py: Python<'py>) -> PyResult> { let len_end_pos = self.data[self.position..].iter().position(|&b| b == b':'); if len_end_pos.is_none() { return Err(PyValueError::new_err("string len not terminated by \":\"")); } let len_end_pos = len_end_pos.unwrap() + self.position; let len_str = std::str::from_utf8(&self.data[self.position..len_end_pos]) .map_err(|_| PyValueError::new_err("invalid length string"))?; // Check for leading zeros in the length if len_str.starts_with('0') && len_str.len() > 1 { return Err(PyValueError::new_err("leading zeros are not allowed")); } let length: usize = len_str .parse() .map_err(|_| PyValueError::new_err("invalid length value"))?; // Skip past the ':' character self.position = len_end_pos + 1; if length > self.data.len() - self.position { return Err(PyValueError::new_err("stream underflow")); } let bytes_slice = &self.data[self.position..self.position + length]; self.position += length; let bytes_obj = PyBytes::new(py, bytes_slice).into_any(); // Return as bytes or decode depending on bytestring_encoding if let Some(encoding) = &self.bytestring_encoding { let encoding_cstr = std::ffi::CString::new(encoding.as_str()) .map_err(|_| PyValueError::new_err("invalid encoding string"))?; Ok( PyString::from_encoded_object(&bytes_obj, Some(&encoding_cstr), Some(c"strict"))? .into_any(), ) } else { Ok(bytes_obj) } } fn decode_list<'py>(&mut self, py: Python<'py>) -> PyResult> { let mut result = Vec::new(); while self.position < self.data.len() && self.data[self.position] != b'e' { let item = self.decode_object(py)?; result.push(item); } if self.position >= self.data.len() { return Err(PyValueError::new_err("malformed list")); } // Skip the 'e' self.position += 1; if self.yield_tuples { let tuple = PyTuple::new(py, &result)?; Ok(tuple.into_any()) } else { let list = PyList::new(py, &result)?; Ok(list.into_any()) } } fn decode_dict<'py>(&mut self, py: Python<'py>) -> PyResult> { let dict = PyDict::new(py); let mut last_key: Option> = None; while self.position < self.data.len() && self.data[self.position] != b'e' { // Keys should be strings only if self.data[self.position] < b'0' || self.data[self.position] > b'9' { return Err(PyValueError::new_err("key was not a simple string")); } // Decode key as bytes let key_obj = self.decode_bytes(py)?; // Get bytes representation for comparison let key_bytes = if let Some(encoding) = &self.bytestring_encoding { if encoding == "utf-8" { let key_str = key_obj.extract::<&str>()?; key_str.as_bytes().to_vec() } else { let key_bytes = key_obj.extract::>()?; key_bytes.as_bytes().to_vec() } } else { let key_bytes = key_obj.extract::>()?; key_bytes.as_bytes().to_vec() }; // Check key ordering if let Some(ref last) = last_key { if last >= &key_bytes { return Err(PyValueError::new_err("dict keys disordered")); } } last_key = Some(key_bytes); // Decode value let value = self.decode_object(py)?; // Insert into dictionary dict.set_item(key_obj, value)?; } if self.position >= self.data.len() { return Err(PyValueError::new_err("malformed dict")); } // Skip the 'e' self.position += 1; Ok(dict) } } #[pyclass] struct Encoder { buffer: Vec, bytestring_encoding: Option, } #[pymethods] impl Encoder { #[new] fn new(_maxsize: Option, bytestring_encoding: Option) -> Self { Encoder { buffer: Vec::new(), bytestring_encoding, } } fn to_bytes<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> { PyBytes::new(py, &self.buffer) } fn process(&mut self, py: Python, x: Bound) -> PyResult<()> { if let Ok(s) = x.extract::>() { self.encode_bytes(s)?; } else if let Ok(n) = x.extract::() { self.encode_int(n)?; } else if let Ok(n) = x.extract::>() { self.encode_long(n)?; } else if x.is_instance_of::() { self.encode_list(py, x)?; } else if x.is_instance_of::() { self.encode_list(py, x)?; } else if let Ok(d) = x.extract::>() { self.encode_dict(py, d)?; } else if let Ok(b) = x.extract::() { self.encode_int(if b { 1 } else { 0 })?; } else if let Ok(obj) = x.extract::>() { self.append_bytes(obj.as_bytes(py)?)?; } else if let Ok(s) = x.extract::<&str>() { self.encode_string(s)?; } else { return Err(PyTypeError::new_err(format!("unsupported type: {:?}", x))); } Ok(()) } fn encode_int(&mut self, x: i64) -> PyResult<()> { let s = format!("i{}e", x); self.buffer.extend(s.as_bytes()); Ok(()) } fn encode_long(&mut self, x: Bound) -> PyResult<()> { let s = format!("i{}e", x.str()?); self.buffer.extend(s.as_bytes()); Ok(()) } fn append_bytes(&mut self, bytes: &[u8]) -> PyResult<()> { self.buffer.extend(bytes); Ok(()) } fn encode_bytes(&mut self, bytes: Bound) -> PyResult<()> { let len_str = format!("{}:", bytes.len()?); self.buffer.extend(len_str.as_bytes()); self.buffer.extend(bytes.as_bytes()); Ok(()) } fn encode_string(&mut self, x: &str) -> PyResult<()> { if let Some(encoding) = &self.bytestring_encoding { if encoding == "utf-8" { let len_str = format!("{}:", x.len()); self.buffer.extend(len_str.as_bytes()); self.buffer.extend(x.as_bytes()); Ok(()) } else { Err(PyTypeError::new_err( "Only utf-8 encoding is supported for string encoding", )) } } else { Err(PyTypeError::new_err( "string found but no encoding specified. Use bencode_utf8 rather bencode?", )) } } fn encode_list(&mut self, py: Python, sequence: Bound) -> PyResult<()> { self.buffer.push(b'l'); for item in sequence.try_iter()? { self.process(py, item?.into())?; } self.buffer.push(b'e'); Ok(()) } fn encode_dict(&mut self, py: Python, dict: Bound) -> PyResult<()> { self.buffer.push(b'd'); // Get all keys and sort them let mut keys: Vec> = dict .keys() .iter() .map(|key| key.extract::>().map_err(|e| e.into())) .collect::>>()?; keys.sort_by(|a, b| { let a_str = a.extract::<&[u8]>().unwrap(); let b_str = b.extract::<&[u8]>().unwrap(); a_str.cmp(b_str) }); for key in keys { if let Ok(bytes) = key.extract::>() { self.encode_bytes(bytes)?; } else { return Err(PyTypeError::new_err("key in dict should be string")); } if let Some(value) = dict.get_item(key)? { self.process(py, value.into())?; } } self.buffer.push(b'e'); Ok(()) } } #[pyfunction] fn bdecode<'py>(py: Python<'py>, s: &Bound) -> PyResult> { let mut decoder = Decoder::new(s, None, None); decoder.decode(py) } #[pyfunction] fn bdecode_as_tuple<'py>(py: Python<'py>, s: &Bound) -> PyResult> { let mut decoder = Decoder::new(s, Some(true), None); decoder.decode(py) } #[pyfunction] fn bdecode_utf8<'py>(py: Python<'py>, s: &Bound) -> PyResult> { let mut decoder = Decoder::new(s, None, Some("utf-8".to_string())); decoder.decode(py) } #[pyfunction] fn bencode(py: Python, x: Bound) -> PyResult> { let mut encoder = Encoder::new(None, None); encoder.process(py, x)?; Ok(encoder.to_bytes(py).into()) } #[pyfunction] fn bencode_utf8(py: Python, x: Bound) -> PyResult> { let mut encoder = Encoder::new(None, Some("utf-8".to_string())); encoder.process(py, x)?; Ok(encoder.to_bytes(py).into()) } #[pymodule] fn _bencode_rs(m: &Bound) -> PyResult<()> { m.add_class::()?; m.add_class::()?; m.add_class::()?; m.add_function(wrap_pyfunction!(bdecode, m)?)?; m.add_function(wrap_pyfunction!(bdecode_as_tuple, m)?)?; m.add_function(wrap_pyfunction!(bdecode_utf8, m)?)?; m.add_function(wrap_pyfunction!(bencode, m)?)?; m.add_function(wrap_pyfunction!(bencode_utf8, m)?)?; Ok(()) } ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1773749457.4150772 fastbencode-0.3.10/tests/0000755000175100017510000000000015156242321014661 5ustar00runnerrunner././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/tests/__init__.py0000644000175100017510000000207715156242311016777 0ustar00runnerrunner# Copyright (C) 2007, 2009, 2010 Canonical Ltd # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # """Tests for fastbencode.""" import unittest def test_suite(): names = [ "test_bencode", ] module_names = ["tests." + name for name in names] result = unittest.TestSuite() loader = unittest.TestLoader() suite = loader.loadTestsFromNames(module_names) result.addTests(suite) return result ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1773749449.0 fastbencode-0.3.10/tests/test_bencode.py0000644000175100017510000004420115156242311017671 0ustar00runnerrunner# Copyright (C) 2007, 2009, 2010, 2016 Canonical Ltd # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA """Tests for bencode structured encoding.""" import copy import sys from unittest import TestCase, TestSuite def get_named_object(module_name, member_name=None): """Get the Python object named by a given module and member name. This is usually much more convenient than dealing with ``__import__`` directly:: >>> doc = get_named_object('pyutils', 'get_named_object.__doc__') >>> doc.splitlines()[0] 'Get the Python object named by a given module and member name.' :param module_name: a module name, as would be found in sys.modules if the module is already imported. It may contain dots. e.g. 'sys' or 'os.path'. :param member_name: (optional) a name of an attribute in that module to return. It may contain dots. e.g. 'MyClass.some_method'. If not given, the named module will be returned instead. :raises: ImportError or AttributeError. """ # We may have just a module name, or a module name and a member name, # and either may contain dots. __import__'s return value is a bit # unintuitive, so we need to take care to always return the object # specified by the full combination of module name + member name. if member_name: # Give __import__ a from_list. It will return the last module in # the dotted module name. attr_chain = member_name.split(".") from_list = attr_chain[:1] obj = __import__(module_name, {}, {}, from_list) for attr in attr_chain: obj = getattr(obj, attr) else: # We're just importing a module, no attributes, so we have no # from_list. __import__ will return the first module in the dotted # module name, so we look up the module from sys.modules. __import__(module_name, globals(), locals(), []) obj = sys.modules[module_name] return obj def iter_suite_tests(suite): """Return all tests in a suite, recursing through nested suites.""" if isinstance(suite, TestCase): yield suite elif isinstance(suite, TestSuite): for item in suite: yield from iter_suite_tests(item) else: raise Exception(f"unknown type {type(suite)!r} for object {suite!r}") def clone_test(test, new_id): """Clone a test giving it a new id. :param test: The test to clone. :param new_id: The id to assign to it. :return: The new test. """ new_test = copy.copy(test) new_test.id = lambda: new_id # XXX: Workaround , which # causes cloned tests to share the 'details' dict. This makes it hard to # read the test output for parameterized tests, because tracebacks will be # associated with irrelevant tests. try: new_test._TestCase__details except AttributeError: # must be a different version of testtools than expected. Do nothing. pass else: # Reset the '__details' dict. new_test._TestCase__details = {} return new_test def apply_scenario(test, scenario): """Copy test and apply scenario to it. :param test: A test to adapt. :param scenario: A tuple describing the scenario. The first element of the tuple is the new test id. The second element is a dict containing attributes to set on the test. :return: The adapted test. """ new_id = f"{test.id()}({scenario[0]})" new_test = clone_test(test, new_id) for name, value in scenario[1].items(): setattr(new_test, name, value) return new_test def apply_scenarios(test, scenarios, result): """Apply the scenarios in scenarios to test and add to result. :param test: The test to apply scenarios to. :param scenarios: An iterable of scenarios to apply to test. :return: result :seealso: apply_scenario """ for scenario in scenarios: result.addTest(apply_scenario(test, scenario)) return result def multiply_tests(tests, scenarios, result): """Multiply tests_list by scenarios into result. This is the core workhorse for test parameterisation. Typically the load_tests() method for a per-implementation test suite will call multiply_tests and return the result. :param tests: The tests to parameterise. :param scenarios: The scenarios to apply: pairs of (scenario_name, scenario_param_dict). :param result: A TestSuite to add created tests to. This returns the passed in result TestSuite with the cross product of all the tests repeated once for each scenario. Each test is adapted by adding the scenario name at the end of its id(), and updating the test object's __dict__ with the scenario_param_dict. >>> import tests.test_sampler >>> r = multiply_tests( ... tests.test_sampler.DemoTest('test_nothing'), ... [('one', dict(param=1)), ... ('two', dict(param=2))], ... TestUtil.TestSuite()) >>> tests = list(iter_suite_tests(r)) >>> len(tests) 2 >>> tests[0].id() 'tests.test_sampler.DemoTest.test_nothing(one)' >>> tests[0].param 1 >>> tests[1].param 2 """ for test in iter_suite_tests(tests): apply_scenarios(test, scenarios, result) return result def permute_tests_for_extension( standard_tests, loader, py_module_name, ext_module_name ): """Helper for permutating tests against an extension module. This is meant to be used inside a modules 'load_tests()' function. It will create 2 scenarios, and cause all tests in the 'standard_tests' to be run against both implementations. Setting 'test.module' to the appropriate module. See tests.test__chk_map.load_tests as an example. :param standard_tests: A test suite to permute :param loader: A TestLoader :param py_module_name: The python path to a python module that can always be loaded, and will be considered the 'python' implementation. (eg '_chk_map_py') :param ext_module_name: The python path to an extension module. If the module cannot be loaded, a single test will be added, which notes that the module is not available. If it can be loaded, all standard_tests will be run against that module. :return: (suite, feature) suite is a test-suite that has all the permuted tests. feature is the Feature object that can be used to determine if the module is available. """ py_module = get_named_object(py_module_name) scenarios = [ ("python", {"module": py_module}), ] suite = loader.suiteClass() try: __import__(ext_module_name) except ModuleNotFoundError: pass else: scenarios.append(("C", {"module": get_named_object(ext_module_name)})) result = multiply_tests(standard_tests, scenarios, suite) return result def load_tests(loader, standard_tests, pattern): return permute_tests_for_extension( standard_tests, loader, "fastbencode._bencode_py", "fastbencode._bencode_rs", ) class RecursionLimit: """Context manager that lowers recursion limit for testing.""" def __init__(self, limit=100) -> None: self._new_limit = limit self._old_limit = sys.getrecursionlimit() def __enter__(self): sys.setrecursionlimit(self._new_limit) return self def __exit__(self, *exc_info): sys.setrecursionlimit(self._old_limit) class TestBencodeDecode(TestCase): module = None def _check(self, expected, source): self.assertEqual(expected, self.module.bdecode(source)) def _run_check_error(self, exc, bad): """Check that bdecoding a string raises a particular exception.""" self.assertRaises(exc, self.module.bdecode, bad) def test_int(self): self._check(0, b"i0e") self._check(4, b"i4e") self._check(123456789, b"i123456789e") self._check(-10, b"i-10e") self._check(int("1" * 1000), b"i" + (b"1" * 1000) + b"e") def test_long(self): self._check(12345678901234567890, b"i12345678901234567890e") self._check(-12345678901234567890, b"i-12345678901234567890e") def test_malformed_int(self): self._run_check_error(ValueError, b"ie") self._run_check_error(ValueError, b"i-e") self._run_check_error(ValueError, b"i-010e") self._run_check_error(ValueError, b"i-0e") self._run_check_error(ValueError, b"i00e") self._run_check_error(ValueError, b"i01e") self._run_check_error(ValueError, b"i-03e") self._run_check_error(ValueError, b"i") self._run_check_error(ValueError, b"i123") self._run_check_error(ValueError, b"i341foo382e") def test_string(self): self._check(b"", b"0:") self._check(b"abc", b"3:abc") self._check(b"1234567890", b"10:1234567890") def test_large_string(self): self.assertRaises(ValueError, self.module.bdecode, b"2147483639:foo") def test_malformed_string(self): self._run_check_error(ValueError, b"10:x") self._run_check_error(ValueError, b"10:") self._run_check_error(ValueError, b"10") self._run_check_error(ValueError, b"01:x") self._run_check_error(ValueError, b"00:") self._run_check_error(ValueError, b"35208734823ljdahflajhdf") self._run_check_error(ValueError, b"432432432432432:foo") self._run_check_error(ValueError, b" 1:x") # leading whitespace self._run_check_error(ValueError, b"-1:x") # negative self._run_check_error(ValueError, b"1 x") # space vs colon self._run_check_error(ValueError, b"1x") # missing colon self._run_check_error(ValueError, (b"1" * 1000) + b":") def test_list(self): self._check([], b"le") self._check([b"", b"", b""], b"l0:0:0:e") self._check([1, 2, 3], b"li1ei2ei3ee") self._check([b"asd", b"xy"], b"l3:asd2:xye") self._check([[b"Alice", b"Bob"], [2, 3]], b"ll5:Alice3:Bobeli2ei3eee") def test_list_deepnested(self): import platform if ( platform.python_implementation() == "PyPy" or sys.version_info[:2] >= (3, 12) or self.id().endswith("(C)") ): expected = [] for i in range(99): expected = [expected] self._check(expected, (b"l" * 100) + (b"e" * 100)) else: with RecursionLimit(): self._run_check_error( RuntimeError, (b"l" * 100) + (b"e" * 100) ) def test_malformed_list(self): self._run_check_error(ValueError, b"l") self._run_check_error(ValueError, b"l01:ae") self._run_check_error(ValueError, b"l0:") self._run_check_error(ValueError, b"li1e") self._run_check_error(ValueError, b"l-3:e") def test_dict(self): self._check({}, b"de") self._check({b"": 3}, b"d0:i3ee") self._check({b"age": 25, b"eyes": b"blue"}, b"d3:agei25e4:eyes4:bluee") self._check( {b"spam.mp3": {b"author": b"Alice", b"length": 100000}}, b"d8:spam.mp3d6:author5:Alice6:lengthi100000eee", ) def test_dict_deepnested(self): if self.id().endswith("(C)"): self.skipTest("no limit recursion in Rust code") with RecursionLimit(): self._run_check_error( RuntimeError, (b"d0:" * 1000) + b"i1e" + (b"e" * 1000) ) def test_malformed_dict(self): self._run_check_error(ValueError, b"d") self._run_check_error(ValueError, b"defoobar") self._run_check_error(ValueError, b"d3:fooe") self._run_check_error(ValueError, b"di1e0:e") self._run_check_error(ValueError, b"d1:b0:1:a0:e") self._run_check_error(ValueError, b"d1:a0:1:a0:e") self._run_check_error(ValueError, b"d0:0:") self._run_check_error(ValueError, b"d0:") self._run_check_error(ValueError, b"d432432432432432432:e") def test_empty_string(self): self.assertRaises(ValueError, self.module.bdecode, b"") def test_junk(self): self._run_check_error(ValueError, b"i6easd") self._run_check_error(ValueError, b"2:abfdjslhfld") self._run_check_error(ValueError, b"0:0:") self._run_check_error(ValueError, b"leanfdldjfh") def test_unknown_object(self): self.assertRaises(ValueError, self.module.bdecode, b"relwjhrlewjh") def test_unsupported_type(self): self._run_check_error(TypeError, 1.5) self._run_check_error(TypeError, None) self._run_check_error(TypeError, lambda x: x) self._run_check_error(TypeError, object) self._run_check_error(TypeError, "ie") def test_decoder_type_error(self): self.assertRaises(TypeError, self.module.bdecode, 1) class TestBdecodeUtf8(TestCase): module = None def _check(self, expected, source): self.assertEqual(expected, self.module.bdecode_utf8(source)) def _run_check_error(self, exc, bad): """Check that bdecoding a string raises a particular exception.""" self.assertRaises(exc, self.module.bdecode_utf8, bad) def test_string(self): self._check("", b"0:") self._check("aäc", b"4:a\xc3\xa4c") self._check("1234567890", b"10:1234567890") def test_large_string(self): self.assertRaises( ValueError, self.module.bdecode_utf8, b"2147483639:foo" ) def test_malformed_string(self): self._run_check_error(ValueError, b"10:x") self._run_check_error(ValueError, b"10:") self._run_check_error(ValueError, b"10") self._run_check_error(ValueError, b"01:x") self._run_check_error(ValueError, b"00:") self._run_check_error(ValueError, b"35208734823ljdahflajhdf") self._run_check_error(ValueError, b"432432432432432:foo") self._run_check_error(ValueError, b" 1:x") # leading whitespace self._run_check_error(ValueError, b"-1:x") # negative self._run_check_error(ValueError, b"1 x") # space vs colon self._run_check_error(ValueError, b"1x") # missing colon self._run_check_error(ValueError, (b"1" * 1000) + b":") def test_empty_string(self): self.assertRaises(ValueError, self.module.bdecode_utf8, b"") def test_invalid_utf8(self): self._run_check_error(UnicodeDecodeError, b"3:\xff\xfe\xfd") class TestBencodeEncode(TestCase): module = None def _check(self, expected, source): self.assertEqual(expected, self.module.bencode(source)) def test_int(self): self._check(b"i4e", 4) self._check(b"i0e", 0) self._check(b"i-10e", -10) def test_long(self): self._check(b"i12345678901234567890e", 12345678901234567890) self._check(b"i-12345678901234567890e", -12345678901234567890) def test_string(self): self._check(b"0:", b"") self._check(b"3:abc", b"abc") self._check(b"10:1234567890", b"1234567890") def test_list(self): self._check(b"le", []) self._check(b"li1ei2ei3ee", [1, 2, 3]) self._check(b"ll5:Alice3:Bobeli2ei3eee", [[b"Alice", b"Bob"], [2, 3]]) def test_list_as_tuple(self): self._check(b"le", ()) self._check(b"li1ei2ei3ee", (1, 2, 3)) self._check(b"ll5:Alice3:Bobeli2ei3eee", ((b"Alice", b"Bob"), (2, 3))) def test_list_deep_nested(self): if self.id().endswith("(C)"): self.skipTest("no limit recursion in Rust code") top = [] lst = top for unused_i in range(1000): lst.append([]) lst = lst[0] with RecursionLimit(): self.assertRaises(RuntimeError, self.module.bencode, top) def test_dict(self): self._check(b"de", {}) self._check(b"d3:agei25e4:eyes4:bluee", {b"age": 25, b"eyes": b"blue"}) self._check( b"d8:spam.mp3d6:author5:Alice6:lengthi100000eee", {b"spam.mp3": {b"author": b"Alice", b"length": 100000}}, ) def test_dict_deep_nested(self): if self.id().endswith("(C)"): self.skipTest("no limit of recursion in Rust code") d = top = {} for i in range(1000): d[b""] = {} d = d[b""] with RecursionLimit(): self.assertRaises(RuntimeError, self.module.bencode, top) def test_bencached(self): self._check(b"i3e", self.module.Bencached(self.module.bencode(3))) def test_invalid_dict(self): self.assertRaises(TypeError, self.module.bencode, {1: b"foo"}) def test_bool(self): self._check(b"i1e", True) self._check(b"i0e", False) class TestBencodeEncodeUtf8(TestCase): module = None def _check(self, expected, source): self.assertEqual(expected, self.module.bencode_utf8(source)) def test_string(self): self._check(b"0:", "") self._check(b"3:abc", "abc") self._check(b"10:1234567890", "1234567890") def test_list(self): self._check(b"le", []) self._check(b"li1ei2ei3ee", [1, 2, 3]) self._check(b"ll5:Alice3:Bobeli2ei3eee", [["Alice", "Bob"], [2, 3]]) def test_list_as_tuple(self): self._check(b"le", ()) self._check(b"li1ei2ei3ee", (1, 2, 3)) self._check(b"ll5:Alice3:Bobeli2ei3eee", (("Alice", "Bob"), (2, 3))) def test_dict(self): self._check(b"de", {}) self._check(b"d3:agei25e4:eyes4:bluee", {b"age": 25, b"eyes": "blue"}) self._check( b"d8:spam.mp3d6:author5:Alice6:lengthi100000eee", {b"spam.mp3": {b"author": b"Alice", b"length": 100000}}, )