pax_global_header00006660000000000000000000000064151151777700014525gustar00rootroot0000000000000052 comment=c3346e8347f1c8dc1d8fbce81634ec92d593b96e python-fastbencode-0.3.8/000077500000000000000000000000001511517777000153315ustar00rootroot00000000000000python-fastbencode-0.3.8/.github/000077500000000000000000000000001511517777000166715ustar00rootroot00000000000000python-fastbencode-0.3.8/.github/dependabot.yaml000066400000000000000000000010311511517777000216550ustar00rootroot00000000000000# Keep GitHub Actions up to date with GitHub's Dependabot... # https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot # https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem version: 2 updates: - package-ecosystem: "github-actions" directory: "/" schedule: interval: weekly - package-ecosystem: "pip" directory: "/" schedule: interval: weekly python-fastbencode-0.3.8/.github/workflows/000077500000000000000000000000001511517777000207265ustar00rootroot00000000000000python-fastbencode-0.3.8/.github/workflows/auto-merge.yml000066400000000000000000000011341511517777000235150ustar00rootroot00000000000000name: Dependabot auto-merge on: pull_request_target permissions: pull-requests: write contents: write jobs: dependabot: runs-on: ubuntu-latest if: ${{ github.actor == 'dependabot[bot]' }} steps: - name: Dependabot metadata id: metadata uses: dependabot/fetch-metadata@v2 with: github-token: "${{ secrets.GITHUB_TOKEN }}" - name: Enable auto-merge for Dependabot PRs run: gh pr merge --auto --squash "$PR_URL" env: PR_URL: ${{github.event.pull_request.html_url}} GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} python-fastbencode-0.3.8/.github/workflows/disperse.yml000066400000000000000000000002741511517777000232720ustar00rootroot00000000000000--- name: Disperse configuration "on": - push jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - uses: jelmer/action-disperse-validate@v2 python-fastbencode-0.3.8/.github/workflows/pythonpackage.yml000066400000000000000000000022731511517777000243120ustar00rootroot00000000000000--- name: Python package "on": [push, pull_request] jobs: build: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] python-version: ['3.10', '3.11', '3.12', '3.13', '3.14'] fail-fast: false steps: - uses: actions/checkout@v6 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v6 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | python -m pip install --upgrade pip cython pip install -U pip ".[dev]" - name: Style checks run: | python -m ruff check . python -m ruff format --check . - name: Test suite run (pure Python) run: | python -m unittest tests.test_suite env: PYTHONHASHSEED: random - name: Run cargo fmt run: | cargo fmt --all -- --check - name: Install in editable mode run: | pip install -e . - name: Test suite run (with Rust extension) run: | python -m unittest tests.test_suite env: PYTHONHASHSEED: random python-fastbencode-0.3.8/.github/workflows/wheels.yaml000066400000000000000000000050351511517777000231040ustar00rootroot00000000000000--- name: Build Python distributions "on": push: pull_request: schedule: - cron: "0 6 * * *" # Daily 6AM UTC build jobs: build-wheels: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] fail-fast: true steps: - uses: actions/checkout@v6 - uses: actions/setup-python@v6 - name: Install dependencies run: | python -m pip install --upgrade pip pip install setuptools wheel cibuildwheel setuptools-rust - name: Set up QEMU uses: docker/setup-qemu-action@v3 if: "matrix.os == 'ubuntu-latest'" - name: Build wheels run: python -m cibuildwheel --output-dir wheelhouse - name: Upload wheels uses: actions/upload-artifact@v5 with: name: artifact-${{ matrix.os }} path: ./wheelhouse/*.whl build-sdist: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - uses: actions/setup-python@v6 - name: Install dependencies run: | python -m pip install --upgrade pip pip install build - name: Build sdist run: python -m build --sdist - name: Upload sdist uses: actions/upload-artifact@v5 with: name: artifact-sdist path: ./dist/*.tar.gz test-sdist: needs: - build-sdist runs-on: ubuntu-latest steps: - uses: actions/setup-python@v6 - name: Install dependencies run: | python -m pip install --upgrade pip # Upgrade packging to avoid a bug in twine. # See https://github.com/pypa/twine/issues/1216 pip install "twine>=6.1.0" "packaging>=24.2" - name: Download sdist uses: actions/download-artifact@v6 with: name: artifact-sdist path: dist - name: Test sdist run: twine check dist/* - name: Test installation from sdist run: pip install dist/*.tar.gz publish: runs-on: ubuntu-latest needs: - build-wheels - build-sdist if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') permissions: id-token: write environment: name: pypi url: https://pypi.org/p/fastbencode steps: - name: Download distributions uses: actions/download-artifact@v6 with: merge-multiple: true pattern: artifact-* path: dist - name: Publish package distributions to PyPI uses: pypa/gh-action-pypi-publish@release/v1 python-fastbencode-0.3.8/.gitignore000066400000000000000000000001631511517777000173210ustar00rootroot00000000000000*.so build __pycache__ fastbencode.egg-info *.pyc dist *~ .mypy_cache *.swp *.swo *.swn # Rust target/ **/*.rs.bk python-fastbencode-0.3.8/CODE_OF_CONDUCT.md000066400000000000000000000064261511517777000201400ustar00rootroot00000000000000# Contributor Covenant Code of Conduct ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socioeconomic status, nationality, personal appearance, race, religion, or sexual identity and orientation. ## Our Standards Examples of behavior that contributes to creating a positive environment include: * Using welcoming and inclusive language * Being respectful of differing viewpoints and experiences * Gracefully accepting constructive criticism * Focusing on what is best for the community * Showing empathy towards other community members Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery and unwelcome sexual attention or advances * Trolling, insulting/derogatory comments, and personal or political attacks * Public or private harassment * Publishing others' private information, such as a physical or electronic address, without explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. ## Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at core@breezy-vcs.org. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html [homepage]: https://www.contributor-covenant.org For answers to common questions about this code of conduct, see https://www.contributor-covenant.org/faq python-fastbencode-0.3.8/COPYING000066400000000000000000000261361511517777000163740ustar00rootroot00000000000000 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. python-fastbencode-0.3.8/Cargo.lock000066400000000000000000000105711511517777000172420ustar00rootroot00000000000000# This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "autocfg" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" [[package]] name = "fastbencode" version = "0.3.8" dependencies = [ "pyo3", ] [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "indoc" version = "2.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706" dependencies = [ "rustversion", ] [[package]] name = "libc" version = "0.2.178" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091" [[package]] name = "memoffset" version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" dependencies = [ "autocfg", ] [[package]] name = "once_cell" version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "portable-atomic" version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483" [[package]] name = "proc-macro2" version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" dependencies = [ "unicode-ident", ] [[package]] name = "pyo3" version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab53c047fcd1a1d2a8820fe84f05d6be69e9526be40cb03b73f86b6b03e6d87d" dependencies = [ "indoc", "libc", "memoffset", "once_cell", "portable-atomic", "pyo3-build-config", "pyo3-ffi", "pyo3-macros", "unindent", ] [[package]] name = "pyo3-build-config" version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b455933107de8642b4487ed26d912c2d899dec6114884214a0b3bb3be9261ea6" dependencies = [ "target-lexicon", ] [[package]] name = "pyo3-ffi" version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c85c9cbfaddf651b1221594209aed57e9e5cff63c4d11d1feead529b872a089" dependencies = [ "libc", "pyo3-build-config", ] [[package]] name = "pyo3-macros" version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0a5b10c9bf9888125d917fb4d2ca2d25c8df94c7ab5a52e13313a07e050a3b02" dependencies = [ "proc-macro2", "pyo3-macros-backend", "quote", "syn", ] [[package]] name = "pyo3-macros-backend" version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03b51720d314836e53327f5871d4c0cfb4fb37cc2c4a11cc71907a86342c40f9" dependencies = [ "heck", "proc-macro2", "pyo3-build-config", "quote", "syn", ] [[package]] name = "quote" version = "1.0.42" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" dependencies = [ "proc-macro2", ] [[package]] name = "rustversion" version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" [[package]] name = "syn" version = "2.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "target-lexicon" version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df7f62577c25e07834649fc3b39fafdc597c0a3527dc1c60129201ccfcbaa50c" [[package]] name = "unicode-ident" version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] name = "unindent" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3" python-fastbencode-0.3.8/Cargo.toml000066400000000000000000000006711511517777000172650ustar00rootroot00000000000000[package] name = "fastbencode" version = "0.3.8" 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.27", features = ["extension-module"] } python-fastbencode-0.3.8/MANIFEST.in000066400000000000000000000002121511517777000170620ustar00rootroot00000000000000include README.md include COPYING include fastbencode/py.typed include Cargo.toml recursive-include tests *.py recursive-include src *.rs python-fastbencode-0.3.8/README.md000066400000000000000000000020051511517777000166050ustar00rootroot00000000000000fastbencode =========== 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 python-fastbencode-0.3.8/SECURITY.md000066400000000000000000000001611511517777000171200ustar00rootroot00000000000000# Security Policy ## Reporting a Vulnerability Please report security issues by e-mail to core@breezy-vcs.org. python-fastbencode-0.3.8/disperse.toml000066400000000000000000000003451511517777000200460ustar00rootroot00000000000000tag-name = "v$VERSION" verify-command = "python3 -m unittest fastbencode.tests.test_suite" tarball-location = [] release-timeout = 5 [[update_version]] path = "fastbencode/__init__.py" new-line = "__version__ = $TUPLED_VERSION" python-fastbencode-0.3.8/fastbencode/000077500000000000000000000000001511517777000176065ustar00rootroot00000000000000python-fastbencode-0.3.8/fastbencode/__init__.py000066400000000000000000000026231511517777000217220ustar00rootroot00000000000000# 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, 7) 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, ) python-fastbencode-0.3.8/fastbencode/_bencode_py.py000066400000000000000000000126141511517777000224320ustar00rootroot00000000000000# 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) python-fastbencode-0.3.8/fastbencode/py.typed000066400000000000000000000000001511517777000212730ustar00rootroot00000000000000python-fastbencode-0.3.8/pyproject.toml000066400000000000000000000047221511517777000202520ustar00rootroot00000000000000[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.14.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 \ """ python-fastbencode-0.3.8/setup.py000077500000000000000000000004631511517777000170510ustar00rootroot00000000000000#!/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, ) ], ) python-fastbencode-0.3.8/src/000077500000000000000000000000001511517777000161205ustar00rootroot00000000000000python-fastbencode-0.3.8/src/lib.rs000066400000000000000000000335571511517777000172510ustar00rootroot00000000000000#![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(()) } python-fastbencode-0.3.8/tests/000077500000000000000000000000001511517777000164735ustar00rootroot00000000000000python-fastbencode-0.3.8/tests/__init__.py000066400000000000000000000020771511517777000206120ustar00rootroot00000000000000# 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 python-fastbencode-0.3.8/tests/test_bencode.py000066400000000000000000000442011511517777000215040ustar00rootroot00000000000000# 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}}, )