diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..e7e7471 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,15 @@ +[run] +plugins = Cython.Coverage +source = bencoder + +[report] +exclude_lines = + pragma: no cover + + def __repr__ + + raise AssertionError + raise NotImplementedError + + if 0: + if __name__ == .__main__.: \ No newline at end of file diff --git a/.github/renovate.json b/.github/renovate.json new file mode 100644 index 0000000..669addd --- /dev/null +++ b/.github/renovate.json @@ -0,0 +1,3 @@ +{ + "extends": ["@whtsky"] +} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..f5fe6e3 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,39 @@ +name: Build + +on: [push, pull_request] + +jobs: + build_wheels: + name: Build wheels on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-20.04, windows-2019, macos-11] + + steps: + - uses: actions/checkout@v4 + + - name: Set up QEMU + if: runner.os == 'Linux' + uses: docker/setup-qemu-action@v3 + with: + platforms: all + + - name: Build wheels + uses: pypa/cibuildwheel@v2.16.2 + env: + CIBW_ARCHS: all + - uses: actions/upload-artifact@v3 + with: + path: ./wheelhouse/*.whl + + make_sdist: + name: Make SDist + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Build SDist + run: pipx run build --sdist + - uses: actions/upload-artifact@v3 + with: + path: dist/*.tar.gz \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..c7a9dbb --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,22 @@ +on: + push: + pull_request: +name: Test +jobs: + pytest: + name: pytest + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + - uses: actions/setup-python@v5 + with: + python-version: "3.10" + architecture: "x64" + - run: python -m pip install -U pip wheel setuptools + - run: python -m pip install -r test-requirements.txt + - run: python -m pip install . + env: + CFLAGS: "-DCYTHON_TRACE=1" + BENCODER_LINETRACE: 1 + - run: python -m pytest --cov=bencoder --cov-report=xml . + - uses: codecov/codecov-action@v3.1.4 diff --git a/.gitignore b/.gitignore index f9bc4bf..0bb041c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__/ *.py[cod] *$py.class +.pytest_cache/ # C extensions *.so @@ -9,6 +10,7 @@ __pycache__/ # Distribution / packaging .Python env/ +venv/ build/ develop-eggs/ dist/ @@ -24,6 +26,8 @@ var/ *.egg-info/ .installed.cfg *.egg +*.whl +.python-version # PyInstaller # Usually these files are written by a python script from a template diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e69de29 diff --git a/.sonarcloud.properties b/.sonarcloud.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/.sonarcloud.properties @@ -0,0 +1 @@ + diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 00d8c20..0000000 --- a/.travis.yml +++ /dev/null @@ -1,20 +0,0 @@ -language: python - -matrix: - include: - - sudo: required - services: - - docker - env: DOCKER_IMAGE=quay.io/pypa/manylinux1_x86_64 - - sudo: required - services: - - docker - env: DOCKER_IMAGE=quay.io/pypa/manylinux1_i686 - PRE_CMD=linux32 - -install: - - docker pull $DOCKER_IMAGE - -script: - - docker run --rm -v `pwd`:/io $DOCKER_IMAGE $PRE_CMD /io/travis/build-wheels.sh - - ls wheelhouse/ \ No newline at end of file diff --git a/MANIFEST.in b/MANIFEST.in index aac7fe1..78b9baf 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,3 @@ -include README.rst LICENSE -recursive-include tests * +include bencoder.pyx README.rst LICENSE +global-exclude *.pyc __pycache__ +recursive-include tests *.py *.torrent diff --git a/README.rst b/README.rst index be3c95c..9917642 100644 --- a/README.rst +++ b/README.rst @@ -3,11 +3,12 @@ Bencoder.pyx A fast bencode implementation in Cython supports both Python2 & Python3 . -.. image:: https://travis-ci.org/whtsky/bencoder.pyx.svg?branch=master - :target: https://travis-ci.org/whtsky/bencoder.pyx - -.. image:: https://ci.appveyor.com/api/projects/status/ur6vy8wdj789oxqv/branch/master?svg=true - :target: https://ci.appveyor.com/project/whtsky/bencoder-pyx +.. image:: https://img.shields.io/pypi/l/bencoder.pyx.svg + :alt: PyPI License + :target: https://pypi.org/project/bencoder.pyx/ +.. image:: https://codecov.io/gh/whtsky/bencoder.pyx/branch/master/graph/badge.svg + :alt: Codecov Coverage + :target: https://codecov.io/gh/whtsky/bencoder.pyx Install ------- @@ -24,7 +25,7 @@ Usage .. code-block:: python - from bencoder import bencode, bdecode + from bencoder import bencode, bdecode, bdecode2 assert bencode("WWWWWW") == b'6:WWWWWW' assert bencode(233) == b'i233e' @@ -32,10 +33,60 @@ Usage with open("debian-8.3.0-amd64-netinst.iso.torrent", "rb") as f: torrent = bdecode(f.read()) print(torrent['announce']) + + decoded, length = bdecode2(b'6:WWWWWWi233e') + assert decoded == b'WWWWWW' + assert length == 8 ChangeLog ---------- +Version 3.0.1 +~~~~~~~~~~~~~~~ ++ Add support for Python 3.11 + +Versoin 3.0.0 +~~~~~~~~~~~~~~~ + ++ Add support for Python 3.9 & 3.10 ++ Drop support for Python 2 ++ Build wheels for musl & aarch64 + +Version 2.0.1 +~~~~~~~~~~~~~~~ + ++ Add support for Python 3.8 ++ Drop support for Python 3.4 + +Version 2.0.0 +~~~~~~~~~~~~~~~ + ++ Use built-in dict instead of OrderedDict on Python >= 3.7 ++ Drop support for Python 3.3 ++ Fix bytes parsing when used with python-future `#41 `_ + +Version 1.2.1 +~~~~~~~~~~~~~~~ + ++ Drop support for Python 2.6 ++ Performance boost for `bencode` method. `#7 `_ + +Version 1.2.0 +~~~~~~~~~~~~~~~ + ++ Add `bdecode2` method. `#6 `_ + +Version 1.1.3 +~~~~~~~~~~~~~~~ + ++ Performance Improvement ++ Fix package metainfo `#3 `_ + +Version 1.1.2 +~~~~~~~~~~~~~~~ + ++ Support encode large int + Version 1.1.0 ~~~~~~~~~~~~~~~ diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100755 index 78735b7..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,83 +0,0 @@ -environment: - global: - # SDK v7.0 MSVC Express 2008's SetEnv.cmd script will fail if the - # /E:ON and /V:ON options are not enabled in the batch script intepreter - # See: http://stackoverflow.com/a/13751649/163740 - CMD_IN_ENV: "cmd /E:ON /V:ON /C .\\appveyor\\run_with_env.cmd" - - matrix: - - # Pre-installed Python versions, which Appveyor may upgrade to - # a later point release. - # See: http://www.appveyor.com/docs/installed-software#python - - - PYTHON: "C:\\Python27" - PYTHON_VERSION: "2.7.x" # currently 2.7.9 - PYTHON_ARCH: "32" - - - PYTHON: "C:\\Python27-x64" - PYTHON_VERSION: "2.7.x" # currently 2.7.9 - PYTHON_ARCH: "64" - - - PYTHON: "C:\\Python33" - PYTHON_VERSION: "3.3.x" # currently 3.3.5 - PYTHON_ARCH: "32" - - - PYTHON: "C:\\Python33-x64" - PYTHON_VERSION: "3.3.x" # currently 3.3.5 - PYTHON_ARCH: "64" - - - PYTHON: "C:\\Python34" - PYTHON_VERSION: "3.4.x" # currently 3.4.3 - PYTHON_ARCH: "32" - - - PYTHON: "C:\\Python34-x64" - PYTHON_VERSION: "3.4.x" # currently 3.4.3 - PYTHON_ARCH: "64" - - # Python versions not pre-installed - - # Python 2.6.6 is the latest Python 2.6 with a Windows installer - # See: https://github.com/ogrisel/python-appveyor-demo/issues/10 - - - PYTHON: "C:\\Python266" - PYTHON_VERSION: "2.6.6" - PYTHON_ARCH: "32" - - - PYTHON: "C:\\Python266-x64" - PYTHON_VERSION: "2.6.6" - PYTHON_ARCH: "64" - - - PYTHON: "C:\\Python35" - PYTHON_VERSION: "3.5.0" - PYTHON_ARCH: "32" - - - PYTHON: "C:\\Python35-x64" - PYTHON_VERSION: "3.5.0" - PYTHON_ARCH: "64" - -init: - - "ECHO Python %PYTHON_VERSION% (%PYTHON_ARCH%bit) from %PYTHON%" - -install: - # Install Python (from the official .msi of http://python.org) and pip when - # not already installed. - - ps: if (-not(Test-Path($env:PYTHON))) { & appveyor\install.ps1 } - - "%PYTHON%\\Scripts\\pip install --disable-pip-version-check --user --upgrade pip wheel cython" - -build_script: - - "%PYTHON%\\python setup.py install" - -test_script: - - "%PYTHON%\\python setup.py test" - -after_test: - - "%PYTHON%\\python setup.py bdist_wheel bdist_wininst bdist_msi" - - ps: "ls dist" - -artifacts: - - path: dist\* - -#on_success: -# - TODO: upload the content of dist/*.whl to a public wheelhouse -# diff --git a/appveyor/install.ps1 b/appveyor/install.ps1 deleted file mode 100755 index 160ba55..0000000 --- a/appveyor/install.ps1 +++ /dev/null @@ -1,229 +0,0 @@ -# Sample script to install Python and pip under Windows -# Authors: Olivier Grisel, Jonathan Helmus, Kyle Kastner, and Alex Willmer -# License: CC0 1.0 Universal: http://creativecommons.org/publicdomain/zero/1.0/ - -$MINICONDA_URL = "http://repo.continuum.io/miniconda/" -$BASE_URL = "https://www.python.org/ftp/python/" -$GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py" -$GET_PIP_PATH = "C:\get-pip.py" - -$PYTHON_PRERELEASE_REGEX = @" -(?x) -(?\d+) -\. -(?\d+) -\. -(?\d+) -(?[a-z]{1,2}\d+) -"@ - - -function Download ($filename, $url) { - $webclient = New-Object System.Net.WebClient - - $basedir = $pwd.Path + "\" - $filepath = $basedir + $filename - if (Test-Path $filename) { - Write-Host "Reusing" $filepath - return $filepath - } - - # Download and retry up to 3 times in case of network transient errors. - Write-Host "Downloading" $filename "from" $url - $retry_attempts = 2 - for ($i = 0; $i -lt $retry_attempts; $i++) { - try { - $webclient.DownloadFile($url, $filepath) - break - } - Catch [Exception]{ - Start-Sleep 1 - } - } - if (Test-Path $filepath) { - Write-Host "File saved at" $filepath - } else { - # Retry once to get the error message if any at the last try - $webclient.DownloadFile($url, $filepath) - } - return $filepath -} - - -function ParsePythonVersion ($python_version) { - if ($python_version -match $PYTHON_PRERELEASE_REGEX) { - return ([int]$matches.major, [int]$matches.minor, [int]$matches.micro, - $matches.prerelease) - } - $version_obj = [version]$python_version - return ($version_obj.major, $version_obj.minor, $version_obj.build, "") -} - - -function DownloadPython ($python_version, $platform_suffix) { - $major, $minor, $micro, $prerelease = ParsePythonVersion $python_version - - if (($major -le 2 -and $micro -eq 0) ` - -or ($major -eq 3 -and $minor -le 2 -and $micro -eq 0) ` - ) { - $dir = "$major.$minor" - $python_version = "$major.$minor$prerelease" - } else { - $dir = "$major.$minor.$micro" - } - - if ($prerelease) { - if (($major -le 2) ` - -or ($major -eq 3 -and $minor -eq 1) ` - -or ($major -eq 3 -and $minor -eq 2) ` - -or ($major -eq 3 -and $minor -eq 3) ` - ) { - $dir = "$dir/prev" - } - } - - if (($major -le 2) -or ($major -le 3 -and $minor -le 4)) { - $ext = "msi" - if ($platform_suffix) { - $platform_suffix = ".$platform_suffix" - } - } else { - $ext = "exe" - if ($platform_suffix) { - $platform_suffix = "-$platform_suffix" - } - } - - $filename = "python-$python_version$platform_suffix.$ext" - $url = "$BASE_URL$dir/$filename" - $filepath = Download $filename $url - return $filepath -} - - -function InstallPython ($python_version, $architecture, $python_home) { - Write-Host "Installing Python" $python_version "for" $architecture "bit architecture to" $python_home - if (Test-Path $python_home) { - Write-Host $python_home "already exists, skipping." - return $false - } - if ($architecture -eq "32") { - $platform_suffix = "" - } else { - $platform_suffix = "amd64" - } - $installer_path = DownloadPython $python_version $platform_suffix - $installer_ext = [System.IO.Path]::GetExtension($installer_path) - Write-Host "Installing $installer_path to $python_home" - $install_log = $python_home + ".log" - if ($installer_ext -eq '.msi') { - InstallPythonMSI $installer_path $python_home $install_log - } else { - InstallPythonEXE $installer_path $python_home $install_log - } - if (Test-Path $python_home) { - Write-Host "Python $python_version ($architecture) installation complete" - } else { - Write-Host "Failed to install Python in $python_home" - Get-Content -Path $install_log - Exit 1 - } -} - - -function InstallPythonEXE ($exepath, $python_home, $install_log) { - $install_args = "/quiet InstallAllUsers=1 TargetDir=$python_home" - RunCommand $exepath $install_args -} - - -function InstallPythonMSI ($msipath, $python_home, $install_log) { - $install_args = "/qn /log $install_log /i $msipath TARGETDIR=$python_home" - $uninstall_args = "/qn /x $msipath" - RunCommand "msiexec.exe" $install_args - if (-not(Test-Path $python_home)) { - Write-Host "Python seems to be installed else-where, reinstalling." - RunCommand "msiexec.exe" $uninstall_args - RunCommand "msiexec.exe" $install_args - } -} - -function RunCommand ($command, $command_args) { - Write-Host $command $command_args - Start-Process -FilePath $command -ArgumentList $command_args -Wait -Passthru -} - - -function InstallPip ($python_home) { - $pip_path = $python_home + "\Scripts\pip.exe" - $python_path = $python_home + "\python.exe" - if (-not(Test-Path $pip_path)) { - Write-Host "Installing pip..." - $webclient = New-Object System.Net.WebClient - $webclient.DownloadFile($GET_PIP_URL, $GET_PIP_PATH) - Write-Host "Executing:" $python_path $GET_PIP_PATH - & $python_path $GET_PIP_PATH - } else { - Write-Host "pip already installed." - } -} - - -function DownloadMiniconda ($python_version, $platform_suffix) { - if ($python_version -eq "3.4") { - $filename = "Miniconda3-3.5.5-Windows-" + $platform_suffix + ".exe" - } else { - $filename = "Miniconda-3.5.5-Windows-" + $platform_suffix + ".exe" - } - $url = $MINICONDA_URL + $filename - $filepath = Download $filename $url - return $filepath -} - - -function InstallMiniconda ($python_version, $architecture, $python_home) { - Write-Host "Installing Python" $python_version "for" $architecture "bit architecture to" $python_home - if (Test-Path $python_home) { - Write-Host $python_home "already exists, skipping." - return $false - } - if ($architecture -eq "32") { - $platform_suffix = "x86" - } else { - $platform_suffix = "x86_64" - } - $filepath = DownloadMiniconda $python_version $platform_suffix - Write-Host "Installing" $filepath "to" $python_home - $install_log = $python_home + ".log" - $args = "/S /D=$python_home" - Write-Host $filepath $args - Start-Process -FilePath $filepath -ArgumentList $args -Wait -Passthru - if (Test-Path $python_home) { - Write-Host "Python $python_version ($architecture) installation complete" - } else { - Write-Host "Failed to install Python in $python_home" - Get-Content -Path $install_log - Exit 1 - } -} - - -function InstallMinicondaPip ($python_home) { - $pip_path = $python_home + "\Scripts\pip.exe" - $conda_path = $python_home + "\Scripts\conda.exe" - if (-not(Test-Path $pip_path)) { - Write-Host "Installing pip..." - $args = "install --yes pip" - Write-Host $conda_path $args - Start-Process -FilePath "$conda_path" -ArgumentList $args -Wait -Passthru - } else { - Write-Host "pip already installed." - } -} - -function main () { - InstallPython $env:PYTHON_VERSION $env:PYTHON_ARCH $env:PYTHON - InstallPip $env:PYTHON -} - -main diff --git a/appveyor/run_with_env.cmd b/appveyor/run_with_env.cmd deleted file mode 100755 index 5da547c..0000000 --- a/appveyor/run_with_env.cmd +++ /dev/null @@ -1,88 +0,0 @@ -:: To build extensions for 64 bit Python 3, we need to configure environment -:: variables to use the MSVC 2010 C++ compilers from GRMSDKX_EN_DVD.iso of: -:: MS Windows SDK for Windows 7 and .NET Framework 4 (SDK v7.1) -:: -:: To build extensions for 64 bit Python 2, we need to configure environment -:: variables to use the MSVC 2008 C++ compilers from GRMSDKX_EN_DVD.iso of: -:: MS Windows SDK for Windows 7 and .NET Framework 3.5 (SDK v7.0) -:: -:: 32 bit builds, and 64-bit builds for 3.5 and beyond, do not require specific -:: environment configurations. -:: -:: Note: this script needs to be run with the /E:ON and /V:ON flags for the -:: cmd interpreter, at least for (SDK v7.0) -:: -:: More details at: -:: https://github.com/cython/cython/wiki/64BitCythonExtensionsOnWindows -:: http://stackoverflow.com/a/13751649/163740 -:: -:: Author: Olivier Grisel -:: License: CC0 1.0 Universal: http://creativecommons.org/publicdomain/zero/1.0/ -:: -:: Notes about batch files for Python people: -:: -:: Quotes in values are literally part of the values: -:: SET FOO="bar" -:: FOO is now five characters long: " b a r " -:: If you don't want quotes, don't include them on the right-hand side. -:: -:: The CALL lines at the end of this file look redundant, but if you move them -:: outside of the IF clauses, they do not run properly in the SET_SDK_64==Y -:: case, I don't know why. -@ECHO OFF - -SET COMMAND_TO_RUN=%* -SET WIN_SDK_ROOT=C:\Program Files\Microsoft SDKs\Windows -SET WIN_WDK=c:\Program Files (x86)\Windows Kits\10\Include\wdf - -:: Extract the major and minor versions, and allow for the minor version to be -:: more than 9. This requires the version number to have two dots in it. -SET MAJOR_PYTHON_VERSION=%PYTHON_VERSION:~0,1% -IF "%PYTHON_VERSION:~3,1%" == "." ( - SET MINOR_PYTHON_VERSION=%PYTHON_VERSION:~2,1% -) ELSE ( - SET MINOR_PYTHON_VERSION=%PYTHON_VERSION:~2,2% -) - -:: Based on the Python version, determine what SDK version to use, and whether -:: to set the SDK for 64-bit. -IF %MAJOR_PYTHON_VERSION% == 2 ( - SET WINDOWS_SDK_VERSION="v7.0" - SET SET_SDK_64=Y -) ELSE ( - IF %MAJOR_PYTHON_VERSION% == 3 ( - SET WINDOWS_SDK_VERSION="v7.1" - IF %MINOR_PYTHON_VERSION% LEQ 4 ( - SET SET_SDK_64=Y - ) ELSE ( - SET SET_SDK_64=N - IF EXIST "%WIN_WDK%" ( - :: See: https://connect.microsoft.com/VisualStudio/feedback/details/1610302/ - REN "%WIN_WDK%" 0wdf - ) - ) - ) ELSE ( - ECHO Unsupported Python version: "%MAJOR_PYTHON_VERSION%" - EXIT 1 - ) -) - -IF %PYTHON_ARCH% == 64 ( - IF %SET_SDK_64% == Y ( - ECHO Configuring Windows SDK %WINDOWS_SDK_VERSION% for Python %MAJOR_PYTHON_VERSION% on a 64 bit architecture - SET DISTUTILS_USE_SDK=1 - SET MSSdk=1 - "%WIN_SDK_ROOT%\%WINDOWS_SDK_VERSION%\Setup\WindowsSdkVer.exe" -q -version:%WINDOWS_SDK_VERSION% - "%WIN_SDK_ROOT%\%WINDOWS_SDK_VERSION%\Bin\SetEnv.cmd" /x64 /release - ECHO Executing: %COMMAND_TO_RUN% - call %COMMAND_TO_RUN% || EXIT 1 - ) ELSE ( - ECHO Using default MSVC build environment for 64 bit architecture - ECHO Executing: %COMMAND_TO_RUN% - call %COMMAND_TO_RUN% || EXIT 1 - ) -) ELSE ( - ECHO Using default MSVC build environment for 32 bit architecture - ECHO Executing: %COMMAND_TO_RUN% - call %COMMAND_TO_RUN% || EXIT 1 -) diff --git a/bencoder.pyx b/bencoder.pyx index fc7e9dc..6bd214d 100644 --- a/bencoder.pyx +++ b/bencoder.pyx @@ -12,21 +12,16 @@ # Based on https://github.com/karamanolev/bencode3/blob/master/bencode.py -__version__ = '1.0.0' -import sys -IS_PY2 = sys.version[0] == '2' -try: - from collections import OrderedDict -except ImportError: - from ordereddict import OrderedDict +from cpython.version cimport PY_MAJOR_VERSION, PY_MINOR_VERSION +END_CHAR = ord('e') +ARRAY_TYPECODE = 'b' -if IS_PY2: - END_CHAR = 'e' +if PY_MAJOR_VERSION >= 3 and PY_MINOR_VERSION >=7: + OrderedDict = dict else: - END_CHAR = ord('e') - + from collections import OrderedDict class BTFailure(Exception): pass @@ -34,7 +29,7 @@ class BTFailure(Exception): def decode_int(bytes x, int f): f += 1 - new_f = x.index(b'e', f) + cdef long new_f = x.index(b'e', f) n = int(x[f:new_f]) if x[f] == b'-'[0]: if x[f + 1] == b'0'[0]: @@ -45,8 +40,8 @@ def decode_int(bytes x, int f): def decode_string(bytes x, int f): - colon = x.index(b':', f) - n = int(x[f:colon]) + cdef long colon = x.index(b':', f) + cdef long n = int(x[f:colon]) if x[f] == b'0'[0] and colon != f + 1: raise ValueError() colon += 1 @@ -62,7 +57,8 @@ def decode_list(bytes x, int f): def decode_dict(bytes x, int f): - r, f = OrderedDict(), f + 1 + r = OrderedDict() + f += 1 while x[f] != END_CHAR: k, f = decode_string(x, f) r[k], f = decode_func[x[f]](x, f) @@ -78,23 +74,23 @@ for func, keys in [ (decode_string, [str(x) for x in range(10)]) ]: for key in keys: - if IS_PY2: - decode_func[key] = func - else: - decode_func[ord(key)] = func + decode_func[ord(key)] = func -def bdecode(bytes x): +def bdecode2(bytes x): try: r, l = decode_func[x[0]](x, 0) except (IndexError, KeyError, ValueError): raise BTFailure("not a valid bencoded string") + return r, l + +def bdecode(bytes x): + r, l = bdecode2(x) if l != len(x): raise BTFailure("invalid bencoded value (data after valid prefix)") return r - -def encode(v, r): +cdef encode(v, list r): tp = type(v) if tp in encode_func: return encode_func[tp](v, r) @@ -102,55 +98,64 @@ def encode(v, r): for tp, func in encode_func.items(): if isinstance(v, tp): return func(v, r) - raise BTFailure("Unknown Type: %s" % tp) + raise BTFailure( + "Can't encode {0}(Type: {1})".format(v, type(v)) + ) + +cdef encode_int(long x, list r): + r.append(b'i') + r.append(str(x).encode()) + r.append(b'e') -def encode_int(int x, list r): - r.extend((b'i', str(x).encode(), b'e')) +cdef encode_long(x, list r): + r.append(b'i') + r.append(str(x).encode()) + r.append(b'e') -def encode_bool(x, list r): - if x: - encode_int(1, r) - else: - encode_int(0, r) +cdef encode_bytes(x, list r): + r.append(str(len(x)).encode()) + r.append(b':') + r.append(x) -def encode_string(x, list r): - if isinstance(x, str): - x = x.encode() - r.extend((str(len(x)).encode(), b':', x)) +cdef encode_string(str x, list r): + r.append(str(len(x)).encode()) + r.append(b':') + r.append(x.encode()) -def encode_list(x, list r): +cdef encode_list(x, list r): r.append(b'l') for i in x: encode(i, r) r.append(b'e') -def encode_dict(x, list r): +cdef encode_dict(x, list r): r.append(b'd') item_list = list(x.items()) item_list.sort() for k, v in item_list: if isinstance(k, str): k = k.encode() - r.extend((str(len(k)).encode(), b':', k)) + encode_bytes(k, r) encode(v, r) r.append(b'e') encode_func = { int: encode_int, - bytes: encode_string, + bool: encode_int, + long: encode_long, + bytes: encode_bytes, str: encode_string, list: encode_list, tuple: encode_list, dict: encode_dict, OrderedDict: encode_dict, - bool: encode_bool, } diff --git a/dev-requirements.txt b/dev-requirements.txt deleted file mode 100644 index ea2e9ad..0000000 --- a/dev-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -nose -cython diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..f890cb0 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,7 @@ +[build-system] +requires = ["cython>=0.29.32", "setuptools>=58.0", "wheel"] +build-backend = "setuptools.build_meta" + +[tool.cibuildwheel] +test-requires = "pytest" +test-command = "pytest {project}/tests" diff --git a/release.sh b/release.sh deleted file mode 100644 index 9167b19..0000000 --- a/release.sh +++ /dev/null @@ -1,3 +0,0 @@ -cython bencoder.pyx -python setup.py sdist --formats=zip,gztar bdist_wheel register -twine upload dist/* diff --git a/setup.cfg b/setup.cfg index af57bc0..838ffa2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1 +1,5 @@ [bdist_wheel] + +[tool:pytest] +testpaths = tests +addopts = -rw diff --git a/setup.py b/setup.py index d437b12..32744ca 100644 --- a/setup.py +++ b/setup.py @@ -1,23 +1,61 @@ -import platform +import os.path +import sys +import os from setuptools import setup from setuptools.extension import Extension +from setuptools.command.test import test as TestCommand -version = platform.python_version_tuple() -install_requires = [] -if version < ('2', '7'): - install_requires.append('ordereddict>=1.1') +pyx_path = 'bencoder.pyx' +c_path = 'bencoder.c' + +if os.path.exists(c_path): + # Remove C file to force Cython recompile. + os.remove(c_path) + +if os.environ.get("BENCODER_LINETRACE", "") == "1": + from Cython.Compiler.Options import get_directive_defaults + directive_defaults = get_directive_defaults() + + directive_defaults['linetrace'] = True + directive_defaults['binding'] = True + +from Cython.Build import cythonize +ext_modules = cythonize(Extension( + "bencoder", + [pyx_path], + extra_compile_args=['-O3'] +)) + + +class PyTest(TestCommand): + user_options = [('pytest-args=', 'a', "Arguments to pass to py.test")] + + def initialize_options(self): + TestCommand.initialize_options(self) + self.pytest_args = [] + + def run_tests(self): + # import here, cause outside the eggs aren't loaded + import coverage + cov = coverage.Coverage() + cov.start() + + import pytest + errno = pytest.main(self.pytest_args) + + cov.stop() + cov.save() + sys.exit(errno) + + +cmdclass = {'test': PyTest} -try: - from Cython.Build import cythonize - ext_modules = cythonize("bencoder.pyx") -except ImportError: - ext_modules = [Extension('bencoder', ['bencoder.c'])] setup( name='bencoder.pyx', - version='1.1.0', + version='3.0.1', description='Yet another bencode implementation in Cython', long_description=open('README.rst', 'r').read(), author='whtsky', @@ -28,26 +66,30 @@ zip_safe=False, include_package_data=True, keywords=['bencoding', 'encode', 'decode', 'bittorrent', 'bencode', 'bencoder', 'cython'], + cmdclass=cmdclass, classifiers=[ + 'Development Status :: 5 - Production/Stable', 'Environment :: Other Environment', 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Cython', 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.6', - 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + 'Programming Language :: Python :: 3.11', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', + 'Intended Audience :: Developers', + 'Topic :: Software Development :: Libraries', 'Topic :: Software Development :: Libraries :: Python Modules', + 'Topic :: Utilities', ], ext_modules=ext_modules, - install_requires=install_requires, - tests_require=['nose'], - test_suite='nose.collector', + install_requires=[], + tests_require=['cython', 'pytest', 'coverage'], ) diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000..0db84cc --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1,3 @@ +Cython==0.29.32 +pytest==7.2.0 +pytest-cov==4.0.0 diff --git a/tests/test_decode.py b/tests/test_decode.py index 48457bf..ed68e67 100644 --- a/tests/test_decode.py +++ b/tests/test_decode.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- -from bencoder import bdecode +from bencoder import bdecode, bdecode2 import os +import sys TORRENT_PATH = os.path.join( os.path.abspath(os.path.dirname(__file__)), @@ -9,6 +10,12 @@ ) +def test_decode2(): + decoded, length = bdecode2(b'6:WWWWWWi233e') + assert decoded == b'WWWWWW' + assert length == 8 + + def test_decode_str(): assert bdecode(b'6:WWWWWW') == b"WWWWWW" @@ -17,6 +24,15 @@ def test_decode_int(): assert bdecode(b'i233e') == 233 +def test_decode_large_int(): + assert bdecode(b'i1455189890e') == 1455189890 + assert bdecode(b'i25735241490e') == 25735241490 + + MAX_SIZE = sys.maxsize + 1 + BENCODED_MAXSIZE = ('i%de' % MAX_SIZE).encode() + assert bdecode(BENCODED_MAXSIZE) == MAX_SIZE + + def test_decode_list(): assert bdecode(b'l1:a1:bi3ee') == [b'a', b'b', 3] diff --git a/tests/test_encode.py b/tests/test_encode.py index 32e08a2..3b2bfa9 100644 --- a/tests/test_encode.py +++ b/tests/test_encode.py @@ -2,6 +2,7 @@ from bencoder import bencode, bdecode import os +import sys TORRENT_PATH = os.path.join( os.path.abspath(os.path.dirname(__file__)), @@ -17,6 +18,15 @@ def test_encode_int(): assert bencode(233) == b'i233e' +def test_encode_large_int(): + assert bencode(1455189890) == b'i1455189890e' + assert bencode(25735241490) == b'i25735241490e' + MAX_SIZE = sys.maxsize + 1 + BENCODED_MAXSIZE = ('i%de' % MAX_SIZE).encode() + + assert bencode(MAX_SIZE) == BENCODED_MAXSIZE + + def test_encode_bytes(): b = b"TheseAreSomeBytes" coded = bencode(b) @@ -24,6 +34,13 @@ def test_encode_bytes(): assert coded == l + b':' + b +def test_encode_string(): + b = "TheseAreSomeString" + coded = bencode(b) + l = str(len(b)) + assert coded == (l + ':' + b).encode() + + def test_encode_list(): assert bencode(['a', 'b', 3]) == b'l1:a1:bi3ee' @@ -32,6 +49,14 @@ def test_encode_tuple(): assert bencode(('a', 'b', 3)) == b'l1:a1:bi3ee' +def test_encode_true(): + assert bencode(True) == bencode(1) + + +def test_encode_false(): + assert bencode(False) == bencode(0) + + def test_encode_dict(): od = dict() od['ka'] = 'va' @@ -43,7 +68,7 @@ def test_encode_dict_subclass(): class AAA(dict): pass - od = dict() + od = AAA() od['ka'] = 'va' od['kb'] = 2 assert bencode(od) == b'd2:ka2:va2:kbi2ee' diff --git a/travis/build-wheels.sh b/travis/build-wheels.sh deleted file mode 100644 index bfe7c00..0000000 --- a/travis/build-wheels.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash -set -e -x - -# Install a system package required by our library -yum install -y atlas-devel - -# Compile wheels -for PYBIN in /opt/python/*/bin; do - ${PYBIN}/pip install -r /io/dev-requirements.txt - ${PYBIN}/pip wheel /io/ -w wheelhouse/ -done - -# Bundle external shared libraries into the wheels -for whl in wheelhouse/*.whl; do - auditwheel repair $whl -w /io/wheelhouse/ -done - -# Install packages and test -for PYBIN in /opt/python/*/bin/; do - ${PYBIN}/pip install bencoder.pyx --no-index -f /io/wheelhouse - ${PYBIN}/nosetests /io/ -done