diff --git a/.flake8 b/.flake8 new file mode 100644 index 00000000..f4546adb --- /dev/null +++ b/.flake8 @@ -0,0 +1,2 @@ +[flake8] +max_line_length = 88 diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 00000000..8c4251a8 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,19 @@ +### What did you do? + +### What did you expect to happen? + +### What actually happened? + +### What versions are you using? + +* OS: +* Python: +* UltraJSON: + +Please include **code** that reproduces the issue. + +The [best reproductions](https://stackoverflow.com/help/minimal-reproducible-example) are [self-contained scripts](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) with minimal dependencies. + +```python +code goes here +``` diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 00000000..7fa46eca --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,7 @@ +Fixes # + +Changes proposed in this pull request: + +* +* +* diff --git a/.github/SECURITY.md b/.github/SECURITY.md new file mode 100644 index 00000000..d453598a --- /dev/null +++ b/.github/SECURITY.md @@ -0,0 +1,6 @@ +# Security Policy + +To report a security issue, either report it privately +[on GitHub](https://github.com/ultrajson/ultrajson/security) (preferred) or +through the [Tidelift security contact](https://tidelift.com/security). Do not +report them on the public issue tracker. diff --git a/.github/labels.yml b/.github/labels.yml new file mode 100644 index 00000000..46a69dde --- /dev/null +++ b/.github/labels.yml @@ -0,0 +1,75 @@ +# Default GitHub labels +- color: d73a4a + description: "Something isn't working" + name: bug +- color: cfd3d7 + description: "This issue or pull request already exists" + name: duplicate +- color: a2eeef + description: "New feature or request" + name: enhancement +- color: 7057ff + description: "Good for newcomers" + name: good first issue +- color: 008672 + description: "Extra attention is needed" + name: help wanted +- color: e4e669 + description: "This doesn't seem right" + name: invalid +- color: d876e3 + description: "Further information is requested" + name: question +- color: ffffff + description: "This will not be worked on" + name: wontfix + +# Keep a Changelog labels +# https://keepachangelog.com/en/1.0.0/ +- color: 0e8a16 + description: "For new features" + name: "changelog: Added" +- color: af99e5 + description: "For changes in existing functionality" + name: "changelog: Changed" +- color: FFA500 + description: "For soon-to-be removed features" + name: "changelog: Deprecated" +- color: 00A800 + description: "For any bug fixes" + name: "changelog: Fixed" +- color: ff0000 + description: "For now removed features" + name: "changelog: Removed" +- color: 045aa0 + description: "In case of vulnerabilities" + name: "changelog: Security" +- color: fbca04 + description: "Exclude PR from release draft" + name: "changelog: skip" + +# Other labels +- color: 0366d6 + description: "For dependencies" + name: dependencies +- color: e28acf + description: "Documentation" + name: documentation +- color: f4660e + description: "" + name: Hacktoberfest +- color: f4660e + description: "To credit accepted Hacktoberfest PRs" + name: hacktoberfest-accepted +- color: caffaf + description: "Deploy and release" + name: release +- color: fbca04 + description: "Unit tests, linting, CI, etc." + name: testing +- color: FFD43B + description: "" + name: Python 2 +- color: F25022 + description: "" + name: Windows diff --git a/.github/release-drafter.yml b/.github/release-drafter.yml new file mode 100644 index 00000000..ba262205 --- /dev/null +++ b/.github/release-drafter.yml @@ -0,0 +1,48 @@ +name-template: "$RESOLVED_VERSION" +tag-template: "$RESOLVED_VERSION" + +categories: + - title: "Added" + labels: + - "changelog: Added" + - "enhancement" + - title: "Changed" + label: "changelog: Changed" + - title: "Deprecated" + label: "changelog: Deprecated" + - title: "Removed" + label: "changelog: Removed" + - title: "Fixed" + labels: + - "changelog: Fixed" + - "bug" + - title: "Security" + label: "changelog: Security" + +exclude-labels: + - "changelog: skip" + +autolabeler: + - label: "changelog: skip" + branch: + - "/pre-commit-ci-update-config/" + +template: | + $CHANGES + +version-resolver: + major: + labels: + - "changelog: Removed" + minor: + labels: + - "changelog: Added" + - "changelog: Changed" + - "changelog: Deprecated" + - "enhancement" + + patch: + labels: + - "changelog: Fixed" + - "bug" + default: minor diff --git a/.github/renovate.json b/.github/renovate.json new file mode 100644 index 00000000..9fb2dca3 --- /dev/null +++ b/.github/renovate.json @@ -0,0 +1,18 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": [ + "config:base" + ], + "labels": [ + "changelog: skip", + "dependencies" + ], + "packageRules": [ + { + "groupName": "github-actions", + "matchManagers": ["github-actions"], + "separateMajorMinor": "false" + } + ], + "schedule": ["on the first day of the month"] +} diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml new file mode 100644 index 00000000..9d4780fc --- /dev/null +++ b/.github/workflows/benchmark.yml @@ -0,0 +1,45 @@ +name: Benchmark + +on: + push: + branches: + - main + pull_request: + paths: + - ".github/workflows/benchmark.yml" + - "tests/benchmark.py" + workflow_dispatch: + +permissions: + contents: read + +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + python-version: ["3.11"] + os: [ubuntu-22.04] + + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - run: git fetch --prune --unshallow + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: ${{ matrix.python-version }} + cache: pip + cache-dependency-path: "setup.py" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install --upgrade orjson simplejson + python -m pip install . + + - name: Tests + shell: bash + run: | + python tests/benchmark.py diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..008b6d54 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,178 @@ +--- +name: Deploy + +on: + push: + branches: + - main + pull_request: + paths: + - ".github/workflows/deploy.yml" + release: + types: + - published + workflow_dispatch: + +permissions: + contents: read + +env: + EXPECTED_DISTS: 93 + EXPECTED_WHEELS_SHA: e8f284ebb38cb5f96df7aefc6a56460bcfc9ac7f + FORCE_COLOR: 1 + +jobs: + build-wheels: + name: "${{ matrix.os }}: ${{ matrix.cibw_arch }} ${{ matrix.manylinux_image }}" + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + include: + - os: windows-latest + cibw_arch: "auto" + - os: windows-11-arm + cibw_arch: "ARM64" + - os: macos-latest + cibw_arch: "x86_64 arm64" + # Linux is built in two batches so we ship both wheel variants (#741). + # Batch 1: manylinux_2_28 (newer gcc, ~+8% faster; modern systems + # prefer it via the higher platform tag). This batch also carries + # musllinux and the i686 build (i686 has no manylinux_2_28 image, so + # it stays manylinux2014 - see CIBW_MANYLINUX_I686_IMAGE below). + - os: ubuntu-latest + cibw_arch: "x86_64 i686" + manylinux_image: manylinux_2_28 + - os: ubuntu-24.04-arm + cibw_arch: aarch64 + manylinux_image: manylinux_2_28 + # Batch 2: manylinux2014 (glibc 2.17 compatibility) for x86_64 and + # aarch64 only; musllinux is skipped as it's already built in batch 1. + - os: ubuntu-latest + cibw_arch: x86_64 + manylinux_image: manylinux2014 + cibw_skip_extra: "*-musllinux_*" + - os: ubuntu-24.04-arm + cibw_arch: aarch64 + manylinux_image: manylinux2014 + cibw_skip_extra: "*-musllinux_*" + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - run: git fetch --prune --unshallow + + - name: Set up Python + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: "3.x" + + # https://github.com/pypa/cibuildwheel + - name: Build wheels + uses: pypa/cibuildwheel@8d2b08b68458a16aeb24b64e68a09ab1c8e82084 # v3.4.1 + with: + output-dir: dist + # Options are supplied via environment variables: + env: + # Build wheels for the currently selected architecture. + CIBW_ARCHS: ${{ matrix.cibw_arch }} + # x86_64 and aarch64 are built once per manylinux image (see matrix) + # so we ship both manylinux2014 and manylinux_2_28 variants (#741). + # Empty (and ignored) on Windows/macOS. + CIBW_MANYLINUX_X86_64_IMAGE: ${{ matrix.manylinux_image }} + CIBW_MANYLINUX_AARCH64_IMAGE: ${{ matrix.manylinux_image }} + CIBW_MANYLINUX_PYPY_X86_64_IMAGE: ${{ matrix.manylinux_image }} + CIBW_MANYLINUX_PYPY_AARCH64_IMAGE: ${{ matrix.manylinux_image }} + # i686 has no manylinux_2_28 image, so it's always manylinux2014. + CIBW_MANYLINUX_I686_IMAGE: manylinux2014 + CIBW_MANYLINUX_PYPY_I686_IMAGE: manylinux2014 + # Include latest Python beta. + CIBW_ENABLE: cpython-prerelease pypy graalpy + # Skip EOL Python versions and macOS x86_64 GraalPy, which times out + # during first-time interpreter discovery on GitHub Actions runners. + # The manylinux2014 batch also skips musllinux (built in the 2_28 batch). + CIBW_SKIP: "gp311* gp*-macosx_x86_64 ${{ matrix.cibw_skip_extra }}" + # Run the test suite after each build. + CIBW_TEST_REQUIRES: "pytest" + CIBW_TEST_COMMAND: pytest {package}/tests + # GraalPy has a bug with progress bar rendering in pip + PIP_PROGRESS_BAR: "off" + + - name: Upload as build artifacts + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: wheels-${{ matrix.os }}-${{ strategy.job-index }} + path: dist/*.whl + + build-sdist-and-upload: + runs-on: ubuntu-latest + needs: ['build-wheels'] + permissions: + # IMPORTANT: this permission is mandatory for trusted publishing + id-token: write + + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - run: | + git fetch --prune --unshallow + + - name: Set up Python + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: "3.x" + + - name: Install dependencies + run: | + python -m pip install -U pip + python -m pip install -U build twine + + - name: Download wheels from build artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + pattern: wheels-* + merge-multiple: true + path: dist-wheels/ + + - name: Build package + run: | + git tag + python -m build --sdist + twine check --strict dist/* + twine check --strict dist-wheels/* + + - name: "What did we get?" + run: | + ls -alR dist dist-wheels + echo "Number of dists, should be $EXPECTED_DISTS:" + files=$(find dist dist-wheels -type f | wc -l) + echo "$files" + [ "$files" -eq "$EXPECTED_DISTS" ] || exit 1 + echo "Wheel fingerprint, should be $EXPECTED_WHEELS_SHA:" + fingerprint=$(find dist-wheels -maxdepth 1 -type f -printf '%f\n' | sed -E 's/^ujson-[^-]+-//' | LC_ALL=C sort | sha1sum | cut -d' ' -f1) + echo "$fingerprint" + [ "$fingerprint" = "$EXPECTED_WHEELS_SHA" ] || exit 1 + + - name: Publish wheels to PyPI + if: github.event.action == 'published' + uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0 + with: + packages-dir: dist-wheels/ + + - name: Publish sdist to PyPI + if: github.event.action == 'published' + uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0 + + - name: Publish wheels to TestPyPI + if: | + github.repository == 'ultrajson/ultrajson' && + github.ref == 'refs/heads/main' + uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0 + with: + repository-url: https://test.pypi.org/legacy/ + packages-dir: dist-wheels/ + + - name: Publish sdist to TestPyPI + if: | + github.repository == 'ultrajson/ultrajson' && + github.ref == 'refs/heads/main' + uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0 + with: + repository-url: https://test.pypi.org/legacy/ diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml new file mode 100644 index 00000000..9d1485f8 --- /dev/null +++ b/.github/workflows/fuzz.yml @@ -0,0 +1,25 @@ +name: Fuzz + +on: [push, pull_request, workflow_dispatch] + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: 3.x + + - name: Install + run: | + python -m pip install -U pip + python -m pip install . + env: + CFLAGS: '-DDEBUG' + + - name: Fuzz + run: python tests/fuzz.py --seed=0:1000 diff --git a/.github/workflows/labels.yml b/.github/workflows/labels.yml new file mode 100644 index 00000000..46d7a076 --- /dev/null +++ b/.github/workflows/labels.yml @@ -0,0 +1,22 @@ +name: Sync labels + +on: + push: + branches: + - main + paths: + - .github/labels.yml + workflow_dispatch: + +jobs: + sync: + permissions: + pull-requests: write + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: micnncim/action-label-syncer@3abd5ab72fda571e69fffd97bd4e0033dd5f495c # v1.3.0 + with: + prune: false + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..0d4b2011 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,40 @@ +name: Lint + +on: [push, pull_request, workflow_dispatch] + +permissions: + contents: read + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: j178/prek-action@bdca6f102f98e2b4c7029491a53dfd366469e33d # v2.0.4 + + stubtest: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - name: Set up Python + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: "3.x" + cache: pip + cache-dependency-path: "setup.py" + + - name: Install dependencies + run: | + python -m pip install -U pip + python -m pip install -U mypy + python -m pip install . + env: + CFLAGS: '-DDEBUG' + + - name: Run stubtest + run: | + # Check that stubs have no omitted / extraneous symbols. + # This does not check the signatures: https://github.com/python/mypy/issues/19261. + python -m mypy.stubtest ujson diff --git a/.github/workflows/release-drafter.yml b/.github/workflows/release-drafter.yml new file mode 100644 index 00000000..7e539aac --- /dev/null +++ b/.github/workflows/release-drafter.yml @@ -0,0 +1,39 @@ +name: Release drafter + +on: + push: + # branches to consider in the event; optional, defaults to all + branches: + - main + # pull_request event is required only for autolabeler + pull_request: + # Only following types are handled by the action, but one can default to all as well + types: [opened, reopened, synchronize] + # pull_request_target event is required for autolabeler to support PRs from forks + # pull_request_target: + # types: [opened, reopened, synchronize] + workflow_dispatch: + +jobs: + update_release_draft: + if: | + github.event.repository.fork == false + && github.event_name != 'pull_request' + permissions: + # write permission is required to create a GitHub Release + contents: write + runs-on: ubuntu-slim + steps: + # Drafts your next release notes as pull requests are merged into "main" + - uses: release-drafter/release-drafter@693d20e7c1ce1a81d3a41962f85914253b518449 # v7.3.1 + + autolabeler: + if: | + github.event.repository.fork == false + && github.event_name == 'pull_request' + permissions: + # write permission is required for autolabeler + pull-requests: write + runs-on: ubuntu-slim + steps: + - uses: release-drafter/release-drafter/autolabeler@693d20e7c1ce1a81d3a41962f85914253b518449 # v7.3.1 diff --git a/.github/workflows/require-pr-label.yml b/.github/workflows/require-pr-label.yml new file mode 100644 index 00000000..bd53a2a8 --- /dev/null +++ b/.github/workflows/require-pr-label.yml @@ -0,0 +1,27 @@ +name: Require PR label + +on: + pull_request: + types: [opened, reopened, labeled, unlabeled, synchronize] + +jobs: + label: + runs-on: ubuntu-latest + + permissions: + issues: write + pull-requests: write + + steps: + - uses: mheap/github-action-required-labels@0ac283b4e65c1fb28ce6079dea5546ceca98ccbe # v5.5.2 + with: + mode: minimum + count: 1 + labels: | + changelog: Added + changelog: Changed + changelog: Deprecated + changelog: Fixed + changelog: Removed + changelog: Security + changelog: skip diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..a216c7b3 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,116 @@ +name: Test + +on: [push, pull_request, workflow_dispatch] + +permissions: + contents: read + +env: + FORCE_COLOR: 1 + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + python-version: + - "graalpy-25" + - "pypy3.11" + - "3.15t" + - "3.15" + - "3.14t" + - "3.14" + - "3.13t" + - "3.13" + - "3.12" + - "3.11" + - "3.10" + os: [ubuntu-latest] + include: + - { python-version: "pypy-3.11", os: windows-latest } + - { python-version: "pypy-3.11", os: macos-latest } + - { python-version: "3.14", os: windows-latest } + - { python-version: "3.14", os: macos-latest } + - { python-version: "3.15", os: windows-latest } + - { python-version: "3.15", os: macos-latest } + + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: ${{ matrix.python-version }} + allow-prereleases: true + cache: pip + cache-dependency-path: "setup.py" + + - name: Install dependencies + run: | + python -m pip install -U pip + python -m pip install -U pytest + python -m pip install . + env: + CFLAGS: '-DDEBUG' + + - name: Tests + run: | + python -m pytest --capture=no + + - name: Test without debug mode + run: | + git clean -Xfd + python -m pip install --force-reinstall . + python -m pytest + + - name: Test with coverage + if: ${{ startsWith(matrix.os, 'ubuntu') && matrix.python-version == '3.10' }} + run: | + pip install -e . + pip install coverage setuptools_scm + CFLAGS="--coverage -O0" CXXFLAGS="--coverage -O0" python setup.py -q build_ext --inplace -f + coverage run -m pytest + ./scripts/coverage.sh + + - name: Upload coverage + if: ${{ startsWith(matrix.os, 'ubuntu') && matrix.python-version == '3.10' }} + uses: codecov/codecov-action@e79a6962e0d4c0c17b229090214935d2e33f8354 # v6.0.1 + with: + plugins: pycoverage + + - name: Test with leak detection + if: "!startsWith(matrix.python-version, 'pypy') && !startsWith(matrix.python-version, 'graalpy') && !endsWith(matrix.python-version, 't')" + run: python -m pytest --leak-max-loops=5000 -v + + cross-arch: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + architecture: [ppc64le, s390x, aarch64, arm/v6, 386] + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - run: git fetch --prune --unshallow + + # https://github.com/docker/setup-qemu-action + - name: Set up QEMU + uses: docker/setup-qemu-action@06116385d9baf250c9f4dcb4858b16962ea869c3 # v4.1.0 + + - name: Test + run: | + docker run -v "$PWD:/io" --platform=linux/${{ matrix.architecture }} python:alpine ash -e -c ' + apk add gcc g++ musl-dev git + cd /io + git config --global --add safe.directory /io + pip install . pytest + FORCE_COLOR=1 pytest + ' + + success: + needs: [test, cross-arch] + runs-on: ubuntu-latest + name: test successful + steps: + - name: Success + run: echo Test successful diff --git a/.github/zizmor.yml b/.github/zizmor.yml new file mode 100644 index 00000000..fd8c5cb2 --- /dev/null +++ b/.github/zizmor.yml @@ -0,0 +1,9 @@ +rules: + artipacked: + ignore: + - benchmark.yml + - deploy.yml + - fuzz.yml + - labels.yml + - lint.yml + - test.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..05a746c9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,153 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +*.cover +*.py,cover +.cache +.coverage +.coverage.* +.hypothesis/ +.nox/ +.pytest_cache/ +.testmondata +.tox/ +cover/ +coverage.xml +htmlcov/ +nosetests.xml + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Generated by setuptools_scm +src/ujson/python/version.h + +# Docker wheel build +pip-cache/ +temp-wheels/ + +# gcov coverage files +cov +*.gcov +*.gcda +*.gcno + +# OS stuff +.fuse_hidden* +.DS_Store diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..66b8d04e --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,78 @@ +repos: + - repo: https://github.com/asottile/pyupgrade + rev: v3.21.2 + hooks: + - id: pyupgrade + args: [--py310-plus] + + - repo: https://github.com/psf/black-pre-commit-mirror + rev: 26.3.1 + hooks: + - id: black + + - repo: https://github.com/PyCQA/isort + rev: 9.0.0a3 + hooks: + - id: isort + + - repo: https://github.com/PyCQA/flake8 + rev: 7.3.0 + hooks: + - id: flake8 + additional_dependencies: [flake8-2020, flake8-implicit-str-concat] + + - repo: https://github.com/pre-commit/pygrep-hooks + rev: v1.10.0 + hooks: + - id: python-check-blanket-noqa + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v6.0.0 + hooks: + - id: check-json + - id: check-merge-conflict + - id: check-toml + - id: check-yaml + - id: end-of-file-fixer + - id: trailing-whitespace + exclude: "^.github/.*_TEMPLATE.md" + + - repo: https://github.com/python-jsonschema/check-jsonschema + rev: 0.37.2 + hooks: + - id: check-github-workflows + - id: check-renovate + + - repo: https://github.com/rhysd/actionlint + rev: v1.7.12 + hooks: + - id: actionlint + + - repo: https://github.com/zizmorcore/zizmor-pre-commit + rev: v1.25.2 + hooks: + - id: zizmor + + - repo: https://github.com/tox-dev/pyproject-fmt + rev: v2.21.2 + hooks: + - id: pyproject-fmt + args: [--max-supported-python=3.15] + + - repo: https://github.com/abravalheri/validate-pyproject + rev: v0.25 + hooks: + - id: validate-pyproject + + - repo: https://github.com/tox-dev/tox-ini-fmt + rev: 1.7.1 + hooks: + - id: tox-ini-fmt + + - repo: meta + hooks: + - id: check-hooks-apply + - id: check-useless-excludes + +ci: + autoupdate_schedule: quarterly diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 290f4222..00000000 --- a/.travis.yml +++ /dev/null @@ -1,11 +0,0 @@ -sudo: false -language: python -python: - - "2.6" - - "2.7" - - "3.3" - - "3.4" -install: - - pip install unittest2 - - python setup.py install -script: python tests/tests.py diff --git a/LICENSE.txt b/LICENSE.txt index 6fc69946..1d00bcb1 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Developed by ESN, an Electronic Arts Inc. studio. +Developed by ESN, an Electronic Arts Inc. studio. Copyright (c) 2014, Electronic Arts Inc. All rights reserved. @@ -16,7 +16,7 @@ derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS INC. BE LIABLE +DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND @@ -24,12 +24,91 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +---- Portions of code from MODP_ASCII - Ascii transformations (upper/lower, etc) -http://code.google.com/p/stringencoders/ -Copyright (c) 2007 Nick Galbreath -- nickg [at] modp [dot] com. All rights reserved. +https://github.com/client9/stringencoders + + Copyright 2005, 2006, 2007 + Nick Galbreath -- nickg [at] modp [dot] com + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + Neither the name of the modp.com nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This is the standard "new" BSD license: + http://www.opensource.org/licenses/bsd-license.php + +https://github.com/client9/stringencoders/blob/cfd5c1507325ae497ea9bacdacba12c0ffd79d30/COPYING + +---- Numeric decoder derived from from TCL library -http://www.opensource.apple.com/source/tcl/tcl-14/tcl/license.terms +https://opensource.apple.com/source/tcl/tcl-14/tcl/license.terms * Copyright (c) 1988-1993 The Regents of the University of California. - * Copyright (c) 1994 Sun Microsystems, Inc. \ No newline at end of file + * Copyright (c) 1994 Sun Microsystems, Inc. + + This software is copyrighted by the Regents of the University of + California, Sun Microsystems, Inc., Scriptics Corporation, ActiveState + Corporation and other parties. The following terms apply to all files + associated with the software unless explicitly disclaimed in + individual files. + + The authors hereby grant permission to use, copy, modify, distribute, + and license this software and its documentation for any purpose, provided + that existing copyright notices are retained in all copies and that this + notice is included verbatim in any distributions. No written agreement, + license, or royalty fee is required for any of the authorized uses. + Modifications to this software may be copyrighted by their authors + and need not follow the licensing terms described here, provided that + the new terms are clearly indicated on the first page of each file where + they apply. + + IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY + FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY + DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE + IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE + NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR + MODIFICATIONS. + + GOVERNMENT USE: If you are acquiring this software on behalf of the + U.S. government, the Government shall have only "Restricted Rights" + in the software and related documentation as defined in the Federal + Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you + are acquiring the software on behalf of the Department of Defense, the + software shall be classified as "Commercial Computer Software" and the + Government shall have only "Restricted Rights" as defined in Clause + 252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the + authors grant the U.S. Government and others acting in its behalf + permission to use and distribute the software in accordance with the + terms specified in this license. diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index 275e06a0..00000000 --- a/MANIFEST.in +++ /dev/null @@ -1,9 +0,0 @@ -include README.rst -include MANIFEST.in -include setup.py -include tests/tests.py -include tests/sample.json -include lib/*.c -include lib/*.h -include python/*.c -include python/*.h \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 00000000..a18e6b9e --- /dev/null +++ b/README.md @@ -0,0 +1,183 @@ +# UltraJSON + +[![PyPI version](https://img.shields.io/pypi/v/ujson.svg?logo=pypi&logoColor=FFE873)](https://pypi.org/project/ujson) +[![Supported Python versions](https://img.shields.io/pypi/pyversions/ujson.svg?logo=python&logoColor=FFE873)](https://pypi.org/project/ujson) +[![PyPI downloads](https://img.shields.io/pypi/dm/ujson.svg)](https://pypistats.org/packages/ujson) +[![GitHub Actions status](https://github.com/ultrajson/ultrajson/workflows/Test/badge.svg)](https://github.com/ultrajson/ultrajson/actions) +[![codecov](https://codecov.io/gh/ultrajson/ultrajson/branch/main/graph/badge.svg)](https://codecov.io/gh/ultrajson/ultrajson) +[![DOI](https://zenodo.org/badge/1418941.svg)](https://zenodo.org/badge/latestdoi/1418941) +[![Code style: Black](https://img.shields.io/badge/code%20style-Black-000000.svg)](https://github.com/psf/black) + +UltraJSON is an ultra fast JSON encoder and decoder written in pure C with bindings for +Python. + +Install with pip: + +```sh +python -m pip install ujson +``` + +## Project status + +> [!WARNING] +> UltraJSON's architecture is fundamentally ill-suited to making changes without +> risk of introducing new security vulnerabilities. As a result, this library +> has been put into a *maintenance-only* mode. Support for new Python versions +> will be added and critical bugs and security issues will still be +> fixed but all other changes will be rejected. Users are encouraged to migrate +> to [orjson](https://pypi.org/project/orjson/) which is both much faster and +> less likely to introduce a surprise buffer overflow vulnerability in the +> future. + +## Usage + +May be used as a drop in replacement for most other JSON parsers for Python: + +```pycon +>>> import ujson +>>> ujson.dumps([{"key": "value"}, 81, True]) +'[{"key":"value"},81,true]' +>>> ujson.loads("""[{"key": "value"}, 81, true]""") +[{'key': 'value'}, 81, True] +``` + +### Encoder options + +#### encode_html_chars + +Used to enable special encoding of "unsafe" HTML characters into safer Unicode +sequences. Default is `False`: + +```pycon +>>> ujson.dumps(" &" + not_html_encoded = '"A string \\\\ \\/ \\b \\f \\n \\r \\t <\\/script> &"' + html_encoded = ( + '"A string \\\\ \\/ \\b \\f \\n \\r \\t \\u003c\\/script\\u003e \\u0026"' + ) + not_slashes_escaped = '"A string \\\\ / \\b \\f \\n \\r \\t &"' + + def helper(expected_output, **encode_kwargs): + output = ujson.encode(test_input, **encode_kwargs) + assert output == expected_output + if encode_kwargs.get("escape_forward_slashes", True): + assert test_input == json.loads(output) + assert test_input == ujson.decode(output) + + # Default behavior assumes encode_html_chars=False. + helper(not_html_encoded, ensure_ascii=True) + helper(not_html_encoded, ensure_ascii=False) + + # Make sure explicit encode_html_chars=False works. + helper(not_html_encoded, ensure_ascii=True, encode_html_chars=False) + helper(not_html_encoded, ensure_ascii=False, encode_html_chars=False) + + # Make sure explicit encode_html_chars=True does the encoding. + helper(html_encoded, ensure_ascii=True, encode_html_chars=True) + helper(html_encoded, ensure_ascii=False, encode_html_chars=True) + + # Do escape forward slashes if disabled. + helper(not_slashes_escaped, escape_forward_slashes=False) + + +def test_write_escaped_string(): + assert "\"\\u003cimg src='\\u0026amp;'\\/\\u003e\"" == ujson.dumps( + "", encode_html_chars=True + ) + + +@pytest.mark.parametrize( + "char, escape", + [ + ("<", "\\u003c"), + (">", "\\u003e"), + ("&", "\\u0026"), + ], +) +def test_html_chars_encoded_individually(char, escape): + encoded = ujson.dumps(char, encode_html_chars=True) + assert escape in encoded.lower() + assert ujson.loads(encoded) == char + + +def test_double_long_issue(): + sut = {"a": -4342969734183514} + encoded = json.dumps(sut) + decoded = json.loads(encoded) + assert sut == decoded + encoded = ujson.encode(sut) + decoded = ujson.decode(encoded) + assert sut == decoded + + +def test_double_long_decimal_issue(): + sut = {"a": -12345678901234.56789012} + encoded = json.dumps(sut) + decoded = json.loads(encoded) + assert sut == decoded + encoded = ujson.encode(sut) + decoded = ujson.decode(encoded) + assert sut == decoded + + +# NOTE: can't match exponents -9 to -5; Python 0-pads +@pytest.mark.parametrize("val", [1e-10, 1e-4, 1e10, 1e15, 1e16, 1e30]) +def test_encode_float_string_rep(val): + assert ujson.dumps(val) == json.dumps(val) + + +def test_encode_decode_long_decimal(): + sut = {"a": -528656961.4399388} + encoded = ujson.dumps(sut) + ujson.decode(encoded) + + +def test_decimal_decode_test(): + sut = {"a": 4.56} + encoded = ujson.encode(sut) + decoded = ujson.decode(encoded) + assert_almost_equal(sut["a"], decoded["a"]) + + +def test_encode_double_conversion(): + test_input = math.pi + output = ujson.encode(test_input) + assert round(test_input, 5) == round(json.loads(output), 5) + assert round(test_input, 5) == round(ujson.decode(output), 5) + + +def test_encode_double_neg_conversion(): + test_input = -math.pi + output = ujson.encode(test_input) + + assert round(test_input, 5) == round(json.loads(output), 5) + assert round(test_input, 5) == round(ujson.decode(output), 5) + + +@pytest.mark.parametrize( + "val", + [ + sys.float_info.max, + -sys.float_info.max, + sys.float_info.min, + sys.float_info.epsilon, + 0.0, + ], +) +def test_encode_float_boundary_values(val): + encoded = ujson.dumps(val) + decoded = ujson.loads(encoded) + if val == 0.0: + assert decoded == 0.0 + elif val > 0: + assert decoded > 0 + else: + assert decoded < 0 + + +def test_encode_array_of_nested_arrays(): + test_input = [[[[]]]] * 20 + output = ujson.encode(test_input) + assert test_input == json.loads(output) + # assert output == json.dumps(test_input) + assert test_input == ujson.decode(output) + + +def test_encode_array_of_doubles(): + test_input = [31337.31337, 31337.31337, 31337.31337, 31337.31337] * 10 + output = ujson.encode(test_input) + assert test_input == json.loads(output) + # assert output == json.dumps(test_input) + assert test_input == ujson.decode(output) + + +def test_encode_string_conversion2(): + test_input = "A string \\ / \b \f \n \r \t" + output = ujson.encode(test_input) + assert test_input == json.loads(output) + assert output == '"A string \\\\ \\/ \\b \\f \\n \\r \\t"' + assert test_input == ujson.decode(output) + + +@pytest.mark.parametrize("codepoint", range(0x00, 0x20)) +def test_encode_control_escaping(codepoint): + # All 32 control characters must roundtrip and agree with stdlib json. + ch = chr(codepoint) + enc = ujson.encode(ch) + assert ujson.decode(enc) == ch + assert enc == json.dumps(ch) + + +# Characters outside of Basic Multilingual Plane(larger than +# 16 bits) are represented as \UXXXXXXXX in python but should be encoded +# as \uXXXX\uXXXX in json. +def test_encode_unicode_bmp(): + s = "\U0001f42e\U0001f42e\U0001f42d\U0001f42d" # 🐮🐮🐭🐭 + encoded = ujson.dumps(s) + encoded_json = json.dumps(s) + + if len(s) == 4: + assert len(encoded) == len(s) * 12 + 2 + else: + assert len(encoded) == len(s) * 6 + 2 + + assert encoded == encoded_json + decoded = ujson.loads(encoded) + assert s == decoded + + # ujson outputs a UTF-8 encoded str object + encoded = ujson.dumps(s, ensure_ascii=False) + + # json outputs an unicode object + encoded_json = json.dumps(s, ensure_ascii=False) + assert len(encoded) == len(s) + 2 # original length + quotes + assert encoded == encoded_json + decoded = ujson.loads(encoded) + assert s == decoded + + +def test_encode_symbols(): + s = "\u273f\u2661\u273f" # ✿♡✿ + encoded = ujson.dumps(s) + encoded_json = json.dumps(s) + assert len(encoded) == len(s) * 6 + 2 # 6 characters + quotes + assert encoded == encoded_json + decoded = ujson.loads(encoded) + assert s == decoded + + # ujson outputs a UTF-8 encoded str object + encoded = ujson.dumps(s, ensure_ascii=False) + + # json outputs an unicode object + encoded_json = json.dumps(s, ensure_ascii=False) + assert len(encoded) == len(s) + 2 # original length + quotes + assert encoded == encoded_json + decoded = ujson.loads(encoded) + assert s == decoded + + +@pytest.mark.parametrize("test_input", [-1, -9223372036854775808, 18446744073709551615]) +def test_encode_special_longs(test_input): + output = ujson.encode(test_input) + + json.loads(output) + ujson.decode(output) + + assert test_input == json.loads(output) + assert output == json.dumps(test_input) + assert test_input == ujson.decode(output) + + +def test_encode_list_conversion(): + test_input = [1, 2, 3, 4] + output = ujson.encode(test_input) + assert test_input == json.loads(output) + assert test_input == ujson.decode(output) + + +def test_encode_dict_conversion(): + test_input = {"k1": 1, "k2": 2, "k3": 3, "k4": 4} + output = ujson.encode(test_input) + assert test_input == json.loads(output) + assert test_input == ujson.decode(output) + assert test_input == ujson.decode(output) + + +@pytest.mark.skipif( + sys.implementation.name in ("pypy", "graalpy"), + reason="PyPy & GraalPy use incompatible GC", +) +def test_encode_dict_values_ref_counting(): + import gc + + gc.collect() + value = ["abc"] + data = {"1": value} + ref_count = sys.getrefcount(value) + ujson.dumps(data) + assert ref_count == sys.getrefcount(value) + + +@pytest.mark.skipif( + sys.implementation.name in ("pypy", "graalpy"), + reason="PyPy & GraalPy use incompatible GC", +) +@pytest.mark.parametrize("key", ["key", b"key", 1, True, False, None]) +@pytest.mark.parametrize("sort_keys", [False, True]) +def test_encode_dict_key_ref_counting(key, sort_keys): + import gc + + gc.collect() + data = {key: "abc"} + ref_count = sys.getrefcount(key) + ujson.dumps(data, sort_keys=sort_keys) + assert ref_count == sys.getrefcount(key) + + +def test_encode_to_utf8(): + test_input = b"\xe6\x97\xa5\xd1\x88".decode("utf-8") + enc = ujson.encode(test_input, ensure_ascii=False) + dec = ujson.decode(enc) + assert enc == json.dumps(test_input, ensure_ascii=False) + assert dec == json.loads(enc) + + +@pytest.mark.parametrize( + "test_input", + [ + '{\n "obj": 31337\n}', + "{}", + "[]", + '{\n "a": {}\n}', + "[\n []\n]", + ], +) +def test_encode_indent(test_input): + obj = ujson.decode(test_input) + output = ujson.encode(obj, indent=4) + assert test_input == output + assert output == json.dumps(obj, indent=4) + + +def test_decode_from_unicode(): + test_input = '{"obj": 31337}' + dec1 = ujson.decode(test_input) + dec2 = ujson.decode(str(test_input)) + assert dec1 == dec2 + + +class O1: + member = 0 + + def toDict(self): + return {"member": self.member} + + +@pytest.mark.skip_leak_test # Known memory leak +def test_encode_recursion_max(): + # 8 is the max recursion depth + test_input = O1() + test_input.member = O1() + test_input.member.member = test_input + with pytest.raises((OverflowError, RecursionError)): + ujson.encode(test_input) + + +def test_encoder_deep_nesting_raises(): + # Deeply nested lists trigger the same recursion limit. + nested = 1 + for _ in range(1025): + nested = [nested] + with pytest.raises(OverflowError): + ujson.dumps(nested) + + +def test_encoder_nesting_just_under_limit(): + nested = 1 + for _ in range(1023): + nested = [nested] + assert ujson.loads(ujson.dumps(nested)) is not None + + +def test_decode_dict(): + test_input = "{}" + obj = ujson.decode(test_input) + assert {} == obj + test_input = '{"one": 1, "two": 2, "three": 3}' + obj = ujson.decode(test_input) + assert {"one": 1, "two": 2, "three": 3} == obj + + +def test_encode_unicode_4_bytes_utf8_fail(): + test_input = b"\xfd\xbf\xbf\xbf\xbf\xbf" + with pytest.raises(OverflowError): + ujson.encode(test_input, reject_bytes=False) + + +def test_encode_null_character(): + test_input = "31337 \x00 1337" + output = ujson.encode(test_input) + assert test_input == json.loads(output) + assert output == json.dumps(test_input) + assert test_input == ujson.decode(output) + + test_input = "\x00" + output = ujson.encode(test_input) + assert test_input == json.loads(output) + assert output == json.dumps(test_input) + assert test_input == ujson.decode(output) + + assert '" \\u0000\\r\\n "' == ujson.dumps(" \u0000\r\n ") + + +def test_decode_null_character(): + test_input = '"31337 \\u0000 31337"' + assert ujson.decode(test_input) == json.loads(test_input) + + +def test_dump_to_file(): + f = io.StringIO() + ujson.dump([1, 2, 3], f) + assert "[1,2,3]" == f.getvalue() + + +def test_dump_load_unicode_roundtrip(): + data = {"emoji": "🐍", "cjk": "日本語", "arabic": "مرحبا"} + buf = io.StringIO() + ujson.dump(data, buf) + buf.seek(0) + assert ujson.load(buf) == data + + +class WritableFileLike: + def __init__(self): + self.bytes = "" + + def write(self, bytes): + self.bytes += bytes + + +def test_dump_to_file_like_object(): + f = WritableFileLike() + ujson.dump([1, 2, 3], f) + assert "[1,2,3]" == f.bytes + + +def test_dump_file_args_error(): + with pytest.raises(TypeError): + ujson.dump([], "") + with pytest.raises(TypeError): + ujson.dump([], "", "") + + +def test_dump_non_callable_write(): + file = types.SimpleNamespace(write="a") + with pytest.raises(TypeError): + ujson.dump([7] * 100, file) + + +def test_failed_dump(): + with pytest.raises(TypeError): + ujson.dump([[0] * 100, object()], io.StringIO()) + + +def test_failed_dump_bogus_file(): + file = types.SimpleNamespace(write=lambda: None) + with pytest.raises(TypeError, match="0 positional arguments"): + ujson.dump([0] * 100, file) + + +def test_failed_dump_failed_write(): + file = types.SimpleNamespace(write=lambda x: 1 / 0) + with pytest.raises(ZeroDivisionError): + ujson.dump([0] * 100, file) + + +def test_failed_dump_closed_file(): + file = io.StringIO() + file.close() + with pytest.raises(ValueError, match="closed file"): + ujson.dump([0] * 100, file) + + +def test_load_file(): + f = io.StringIO("[1,2,3,4]") + assert [1, 2, 3, 4] == ujson.load(f) + + +class ReadableFileLike: + def read(self): + try: + self.end + except AttributeError: + self.end = True + return "[1,2,3,4]" + + +def test_load_file_like_object(): + f = ReadableFileLike() + assert [1, 2, 3, 4] == ujson.load(f) + + +def test_load_file_args_error(): + with pytest.raises(TypeError): + ujson.load("[]") + + +def test_version(): + assert re.search( + r"^\d+\.\d+(\.\d+)?", ujson.__version__ + ), "ujson.__version__ must be a string like '1.4.0'" + + +def test_decode_number_with32bit_sign_bit(): + # Test that numbers that fit within 32 bits but would have the + # sign bit set (2**31 <= x < 2**32) are decoded properly. + docs = ( + '{"id": 3590016419}', + '{"id": %s}' % 2**31, + '{"id": %s}' % 2**32, + '{"id": %s}' % ((2**32) - 1), + ) + results = (3590016419, 2**31, 2**32, 2**32 - 1) + for doc, result in zip(docs, results): + assert ujson.decode(doc)["id"] == result + + +def test_encode_big_escape(): + for x in range(10): + base = "\u00e5".encode() + test_input = base * 1024 * 1024 * 2 + ujson.encode(test_input, reject_bytes=False) + + +def test_decode_big_escape(): + for x in range(10): + base = "\u00e5".encode() + quote = b'"' + test_input = quote + (base * 1024 * 1024 * 2) + quote + ujson.decode(test_input) + + +class DictTest: + def toDict(self): + return dict(key=31337) + + def __json__(self): + return '"json defined"' # Fallback and shouldn't be called. + + +def test_to_dict(): + o = DictTest() + output = ujson.encode(o) + dec = ujson.decode(output) + assert dec == {"key": 31337} + + +class _PlainObject: + pass + + +def test_default_function_fallthrough(): + # Objects with neither toDict nor __json__ fall through to default=. + assert ( + ujson.loads(ujson.dumps(_PlainObject(), default=lambda o: "fallback")) + == "fallback" + ) + + +class JSONTest: + def __init__(self, output): + self.output = output + + def __json__(self): + return copy.deepcopy(self.output) + + +def test_object_with_json(): + # If __json__ returns a string, then that string + # will be used as a raw JSON snippet in the object. + d = {"key": JSONTest('"this is the correct output"')} + output = ujson.encode(d) + dec = ujson.decode(output) + assert dec == {"key": "this is the correct output"} + + +def test_object_with_complex_json(): + # If __json__ returns a string, then that string + # will be used as a raw JSON snippet in the object. + obj = {"foo": ["bar", "baz"]} + d = {"key": JSONTest(ujson.dumps(obj))} + output = ujson.encode(d) + dec = ujson.decode(output) + assert dec == {"key": obj} + + +class _BytesJSON: + def __json__(self): + return b'{"source": "bytes"}' + + +def test_object_with_json_bytes(): + # __json__ may return bytes; they are treated as raw JSON just like str. + assert ujson.loads(ujson.dumps(_BytesJSON())) == {"source": "bytes"} + + +def test_object_with_json_type_error(): + # __json__ must return a string, otherwise it should raise an error. + for return_value in (None, 1234, 12.34, True, {}): + d = {"key": JSONTest(return_value)} + with pytest.raises(TypeError): + ujson.encode(d) + + +class JSONTestAttributeError: + def __json__(self): + raise AttributeError + + +def test_object_with_json_attribute_error(): + # If __json__ raises an error, make sure python actually raises it. + d = {"key": JSONTestAttributeError()} + with pytest.raises(AttributeError): + ujson.encode(d) + + +class JSONtoDict: + def __init__(self, value): + self.value = value + + def toDict(self): + return copy.deepcopy(self.value) + + +def test_object_with_to_dict_type_error(): + # toDict must return a dict, otherwise it should raise an error. + for return_value in (None, 1234, 12.34, True, "json"): + d = {"key": JSONtoDict(return_value)} + with pytest.raises(TypeError): + ujson.encode(d) + + +class JSONtoDictAttributeError: + def toDict(self): + raise AttributeError + + +def test_object_with_to_dict_attribute_error(): + # If toDict raises an error, make sure python actually raises it. + d = {"key": JSONTestAttributeError()} + with pytest.raises(AttributeError): + ujson.encode(d) + + +def test_decode_array_empty(): + test_input = "[]" + obj = ujson.decode(test_input) + assert [] == obj + + +def test_encode_surrogate_characters(): + assert ujson.dumps("\udc7f") == r'"\udc7f"' + out = r'{"\ud800":"\udfff"}' + assert ujson.dumps({"\ud800": "\udfff"}) == out + assert ujson.dumps({"\ud800": "\udfff"}, sort_keys=True) == out + o = {b"\xed\xa0\x80": b"\xed\xbf\xbf"} + assert ujson.dumps(o, reject_bytes=False) == out + assert ujson.dumps(o, reject_bytes=False, sort_keys=True) == out + + out2 = '{"\ud800":"\udfff"}' + assert ujson.dumps({"\ud800": "\udfff"}, ensure_ascii=False) == out2 + assert ujson.dumps({"\ud800": "\udfff"}, ensure_ascii=False, sort_keys=True) == out2 + + +@pytest.mark.xfail( + sys.implementation.name == "graalpy", + reason="GraalPy bug, it fails the last example.", +) +@pytest.mark.parametrize( + "test_input, expected", + [ + # Normal cases + (r'"\uD83D\uDCA9"', "\U0001f4a9"), + (r'"a\uD83D\uDCA9b"', "a\U0001f4a9b"), + # Unpaired surrogates + (r'"\uD800"', "\ud800"), + (r'"a\uD800b"', "a\ud800b"), + (r'"\uDEAD"', "\udead"), + (r'"a\uDEADb"', "a\udeadb"), + (r'"\uD83D\uD83D\uDCA9"', "\ud83d\U0001f4a9"), + (r'"\uDCA9\uD83D\uDCA9"', "\udca9\U0001f4a9"), + (r'"\uD83D\uDCA9\uD83D"', "\U0001f4a9\ud83d"), + (r'"\uD83D\uDCA9\uDCA9"', "\U0001f4a9\udca9"), + (r'"\uD83D \uDCA9"', "\ud83d \udca9"), + # No decoding of actual surrogate characters (rather than escaped ones) + ('"\ud800"', "\ud800"), + ('"\udead"', "\udead"), + ('"\ud800a\udead"', "\ud800a\udead"), + ('"\ud83d\udca9"', "\ud83d\udca9"), + ], +) +def test_decode_surrogate_characters(test_input, expected): + assert ujson.loads(test_input) == expected + assert ujson.loads(test_input.encode("utf-8", "surrogatepass")) == expected + + # Ensure that this matches stdlib's behaviour + assert json.loads(test_input) == expected + + +def test_sort_keys(): + data = {"a": 1, "c": 1, "b": 1, "e": 1, "f": 1, "d": 1} + sorted_keys = ujson.dumps(data, sort_keys=True) + assert sorted_keys == '{"a":1,"b":1,"c":1,"d":1,"e":1,"f":1}' + + +def test_sort_keys_unordered(): + data = {"a": 1, 1: 2, None: 3} + assert ujson.dumps(data) == '{"a":1,"1":2,"null":3}' + with pytest.raises(TypeError): + ujson.dumps(data, sort_keys=True) + + +@pytest.mark.parametrize( + "test_input", + [ + "[31337]", # array one item + "18446744073709551615", # long unsigned value + "9223372036854775807", # big value + "-9223372036854775808", # small value + "{}\n\t ", # trailing whitespaces + ], +) +def test_decode_no_assert(test_input): + ujson.decode(test_input) + + +@pytest.mark.parametrize( + "test_input, expected", + [ + ("31337", 31337), + ("-31337", -31337), + ("100000000000000000000.0", 1e20), + ("-0", 0), # negative zero integer → plain 0 + ("-0.0", -0.0), # negative zero float preserves sign bit + ], +) +def test_decode(test_input, expected): + result = ujson.decode(test_input) + assert result == expected + if test_input == "-0.0": + assert math.copysign(1.0, result) < 0 + + +@pytest.mark.parametrize( + "test_input", + [ + "1337E40", + "1.337E40", + "1337E+9", + "1.337e+40", + "1337E40", + "1337e40", + "1.337E-4", + "1.337e-4", + ], +) +def test_decode_numeric_int_exp(test_input): + output = ujson.decode(test_input) + assert output == json.loads(test_input) + + +@pytest.mark.parametrize( + "i", + [ + -(10**25), # very negative + -(2**64), # too large in magnitude for a uint64 + -(2**63) - 1, # too small for a int64 + 2**64, # too large for a uint64 + 10**25, # very positive + 2**100, # well beyond 64-bit range + -(2**100), + 2**200, + ], +) +@pytest.mark.parametrize("mode", ["encode", "decode"]) +@pytest.mark.skip_leak_test # To be fixed separately +def test_encode_decode_big_int(i, mode): + # Test ints that are too large to be represented by a C integer type + for python_object in (i, [i], {"i": i}): + json_string = json.dumps(python_object, separators=(",", ":")) + if mode == "encode": + if hasattr(sys, "pypy_version_info"): + # https://foss.heptapod.net/pypy/pypy/-/issues/3765 + pytest.skip("PyPy can't serialise big ints") + assert ujson.encode(python_object) == json_string + if isinstance(python_object, dict): + assert ujson.encode(python_object, sort_keys=True) == json_string + else: + assert ujson.decode(json_string) == python_object + + +@pytest.mark.skipif( + not hasattr(sys, "set_int_max_str_digits"), + reason="sys.set_int_max_str_digits backported to 3.11, 3.10.7+, 3.9.14+", +) +@pytest.mark.xfail( + sys.implementation.name == "pypy", + reason="PyPy's PyNumber_ToBase ignores sys.get_int_max_str_digits()", +) +def test_encode_too_big_int_error(): + with pytest.raises(ValueError, match="integer string conversion"): + ujson.dumps(pow(10, 10_000)) + + +@pytest.mark.skipif( + not hasattr(sys, "set_int_max_str_digits"), + reason="sys.set_int_max_str_digits backported to 3.11, 3.10.7+, 3.9.14+", +) +def test_decode_too_big_int_error(): + with pytest.raises(ValueError, match="integer string conversion"): + ujson.loads("9" * 10_000) + + +def test_decode_integer_near_str_digit_limit(): + """ + sys.set_int_max_str_digits (default 4300) was introduced in Python 3.11 + and backported as a security fix to 3.10.7, 3.9.14, and 3.8.14 + (CVE-2020-10735). ujson's PyLong_FromString path inherits this limit. + + When the limit is present: exactly 4300 digits succeeds; 4301 raises. + When absent (e.g. 3.10.0–3.10.6): both digit counts decode successfully. + PyPy does not enforce the limit even when the attribute exists. + """ + at_limit = "9" * 4300 + over_limit = "9" * 4301 + + assert ujson.loads(at_limit) == int(at_limit) + + if hasattr(sys, "set_int_max_str_digits"): + with pytest.raises(ValueError, match="[Ll]imit|digits"): + ujson.loads(over_limit) + else: + assert ujson.loads(over_limit) == int(over_limit) + + +@pytest.mark.parametrize( + "test_input, expected", + [ + ('{{1337:""}}', ujson.JSONDecodeError), # broken dict key type leak test + ('{{"key":"}', ujson.JSONDecodeError), # broken dict leak test + ('{{"key":"}', ujson.JSONDecodeError), # broken dict leak test + ("[[[true", ujson.JSONDecodeError), # broken list leak test + ], +) +def test_decode_range_raises(test_input, expected): + for x in range(1000): + with pytest.raises(expected): + ujson.decode(test_input) + + +@pytest.mark.parametrize( + "test_input, expected", + [ + ("fdsa sda v9sa fdsa", ujson.JSONDecodeError), # gibberish + ("[", ujson.JSONDecodeError), # broken array start + ("{", ujson.JSONDecodeError), # broken object start + ("]", ujson.JSONDecodeError), # broken array end + ("}", ujson.JSONDecodeError), # broken object end + ('{"one":1,}', ujson.JSONDecodeError), # object trailing comma fail + ('"TESTING', ujson.JSONDecodeError), # string unterminated + ('"TESTING\\"', ujson.JSONDecodeError), # string bad escape + ("tru", ujson.JSONDecodeError), # true broken + ("fa", ujson.JSONDecodeError), # false broken + ("n", ujson.JSONDecodeError), # null broken + ("{{{{31337}}}}", ujson.JSONDecodeError), # dict with no key + ('{{{{"key"}}}}', ujson.JSONDecodeError), # dict with no colon or value + ('{{{{"key":}}}}', ujson.JSONDecodeError), # dict with no value + ("[31337,]", ujson.JSONDecodeError), # array trailing comma fail + ("[,31337]", ujson.JSONDecodeError), # array leading comma fail + ("[,]", ujson.JSONDecodeError), # array only comma fail + ("[]]", ujson.JSONDecodeError), # array unmatched bracket fail + ("{}\n\t a", ujson.JSONDecodeError), # with trailing non whitespaces + ('{"age", 44}', ujson.JSONDecodeError), # read bad object syntax + ('"\\q"', ujson.JSONDecodeError), # unrecognised escape letter + ('"\\x41"', ujson.JSONDecodeError), # hex escape (Python syntax, not JSON) + ('"\\0"', ujson.JSONDecodeError), # octal-style escape (not JSON) + ('"\\a"', ujson.JSONDecodeError), # Python bell escape (not JSON) + ], +) +def test_decode_raises(test_input, expected): + with pytest.raises(expected): + ujson.decode(test_input) + + +@pytest.mark.parametrize( + "test_input, expected", + [ + ("[", ujson.JSONDecodeError), # array depth too big + ("{", ujson.JSONDecodeError), # object depth too big + ], +) +def test_decode_raises_for_long_input(test_input, expected): + with pytest.raises(expected): + ujson.decode(test_input * (1024 * 1024)) + + +def test_decode_depth_limit_arrays(): + # 1025 levels of nesting exceeds JSON_MAX_OBJECT_DEPTH. + limit = 1025 + deep = "[" * limit + "1" + "]" * limit + with pytest.raises(ujson.JSONDecodeError, match="[Dd]epth|[Ll]imit"): + ujson.loads(deep) + + +def test_decode_just_under_depth_limit(): + depth = 1024 + deep = "[" * depth + "1" + "]" * depth + result = ujson.loads(deep) + for _ in range(depth): + result = result[0] + assert result == 1 + + +def test_decode_exception_is_value_error(): + assert issubclass(ujson.JSONDecodeError, ValueError) + assert ujson.JSONDecodeError is not ValueError + + +@pytest.mark.parametrize( + "test_input, expected", + [ + (True, "true"), + (False, "false"), + (None, "null"), + ([True, False, None], "[true,false,null]"), + ((True, False, None), "[true,false,null]"), + ], +) +def test_dumps(test_input, expected): + assert ujson.dumps(test_input) == expected + + +class SomeObject: + def __init__(self, message, exception=None): + self._message = message + self._exception = exception + + def __repr__(self): + if self._exception: + raise self._exception + return self._message + + +@pytest.mark.parametrize( + "test_input, expected_exception, expected_message", + [ + (set(), TypeError, "set() is not JSON serializable"), + ({1, 2, 3}, TypeError, "{1, 2, 3} is not JSON serializable"), + (SomeObject("Some Object"), TypeError, "Some Object is not JSON serializable"), + (SomeObject("\ud800"), UnicodeEncodeError, None), + (SomeObject(None, KeyboardInterrupt), KeyboardInterrupt, None), + ], +) +def test_dumps_raises(test_input, expected_exception, expected_message): + with pytest.raises(expected_exception) as e: + ujson.dumps(test_input) + if expected_message: + assert str(e.value) == expected_message + + +@pytest.mark.parametrize( + "test_input", + [ + float("nan"), + float("inf"), + -float("inf"), + [float("nan")], # nested in list + {"k": float("inf")}, # nested in dict value + [[float("nan")]], # doubly nested + {"a": {"b": float("inf")}}, + ], +) +def test_encode_raises_allow_nan(test_input): + with pytest.raises(OverflowError): + ujson.dumps(test_input, allow_nan=False) + + +def test_nan_inf_support(): + # Test ported from pandas + text = '["a", NaN, "NaN", Infinity, "Infinity", -Infinity, "-Infinity"]' + data = ujson.loads(text) + expected = [ + "a", + float("nan"), + "NaN", + float("inf"), + "Infinity", + -float("inf"), + "-Infinity", + ] + for a, b in zip(data, expected): + assert a == b or math.isnan(a) and math.isnan(b) + + +def test_special_singletons(): + pos_inf = ujson.loads("Infinity") + neg_inf = ujson.loads("-Infinity") + nan = ujson.loads("NaN") + null = ujson.loads("null") + assert math.isinf(pos_inf) and pos_inf > 0 + assert math.isinf(neg_inf) and neg_inf < 0 + assert math.isnan(nan) + assert null is None + + +@pytest.mark.parametrize( + "test_input, expected_message", + [ + ("n", "Unexpected character .* 'null'"), + ("N", "Unexpected character .*'NaN'"), + ("NA", "Unexpected character .* 'NaN'"), + ("Na N", "Unexpected character .* 'NaN'"), + ("nan", "Unexpected character .* 'null'"), + ("none", "Unexpected character .* 'null'"), + ("i", "Expected object or value"), + ("I", "Unexpected character .* 'Infinity'"), + ("Inf", "Unexpected character .* 'Infinity'"), + ("InfinitY", "Unexpected character .* 'Infinity'"), + ("-i", "Trailing data"), + ("-I", "Unexpected character .* '-Infinity'"), + ("-Inf", "Unexpected character .* '-Infinity'"), + ("-InfinitY", "Unexpected character .* '-Infinity'"), + ("- i", "Trailing data"), + ("- I", "Trailing data"), + ("- Inf", "Trailing data"), + ("- InfinitY", "Trailing data"), + ], +) +def test_incomplete_special_inputs(test_input, expected_message): + with pytest.raises(ujson.JSONDecodeError, match=expected_message): + ujson.loads(test_input) + + +@pytest.mark.parametrize( + "test_input, expected_message", + [ + ("NaNaNaN", "Trailing data"), + ("Infinity and Beyond", "Trailing data"), + ("-Infinity-and-Beyond", "Trailing data"), + ("NaN!", "Trailing data"), + ("Infinity!", "Trailing data"), + ("-Infinity!", "Trailing data"), + ], +) +def test_overcomplete_special_inputs(test_input, expected_message): + with pytest.raises(ujson.JSONDecodeError, match=expected_message): + ujson.loads(test_input) + + +@pytest.mark.parametrize( + "test_input", + [ + { + "key1": "value1", + "key1": "value1", + "key1": "value1", + "key1": "value1", + "key1": "value1", + "key1": "value1", + }, + { + "بن": "value1", + "بن": "value1", + "بن": "value1", + "بن": "value1", + "بن": "value1", + "بن": "value1", + "بن": "value1", + }, + ], +) +def test_encode_no_assert(test_input): + ujson.encode(test_input) + + +@pytest.mark.parametrize( + "test_input, expected", + [ + (1.0, "1.0"), + (OrderedDict([(1, 1), (0, 0), (8, 8), (2, 2)]), '{"1":1,"0":0,"8":8,"2":2}'), + ({"a": float("NaN")}, '{"a":NaN}'), + ({"a": float("inf")}, '{"a":Infinity}'), + ({"a": -float("inf")}, '{"a":-Infinity}'), + ], +) +def test_encode(test_input, expected): + assert ujson.encode(test_input) == expected + + +@pytest.mark.parametrize( + "test_input", + [ + [ + 9223372036854775807, + 9223372036854775807, + 9223372036854775807, + 9223372036854775807, + 9223372036854775807, + 9223372036854775807, + ], + [ + 18446744073709551615, + 18446744073709551615, + 18446744073709551615, + ], + ], +) +def test_encode_list_long_conversion(test_input): + output = ujson.encode(test_input) + assert test_input == json.loads(output) + assert test_input == ujson.decode(output) + + +@pytest.mark.parametrize( + "test_input", + [ + 9223372036854775807, + 18446744073709551615, + ], +) +def test_encode_long_conversion(test_input): + output = ujson.encode(test_input) + + assert test_input == json.loads(output) + assert output == json.dumps(test_input) + assert test_input == ujson.decode(output) + + +@pytest.mark.parametrize( + "test_input", + [ + [[[[]]]], + 31337, + -31337, + None, + True, + False, + ], +) +def test_encode_decode(test_input): + output = ujson.encode(test_input) + + assert test_input == json.loads(output) + assert output == json.dumps(test_input) + assert test_input == ujson.decode(output) + + +@pytest.mark.parametrize( + "test_input", + [ + "Räksmörgås اسامة بن محمد بن عوض بن لادن", + "\xe6\x97\xa5\xd1\x88", + "\xf0\x90\x8d\x86", # surrogate pair + "\xf0\x91\x80\xb0TRAILINGNORMAL", # 4 bytes UTF8 + "\xf3\xbf\xbf\xbfTRAILINGNORMAL", # 4 bytes UTF8 highest + ], +) +def test_encode_unicode(test_input): + enc = ujson.encode(test_input) + dec = ujson.decode(enc) + + assert enc == json.dumps(test_input) + assert dec == json.loads(enc) + + +@pytest.mark.parametrize( + "codepoint, expected_ord", + [ + ("\\uFFFF", 0xFFFF), # highest BMP non-surrogate + ("\\u0080", 0x0080), # first codepoint needing a 2-byte UTF-8 sequence + ("\\u0800", 0x0800), # first codepoint needing a 3-byte UTF-8 sequence + ], +) +def test_unicode_boundary_codepoints(codepoint, expected_ord): + decoded = ujson.loads(f'"{codepoint}"') + assert ord(decoded) == expected_ord + + +@pytest.mark.parametrize( + "test_input, expected", + [ + ("-1.1234567893", -1.1234567893), + ("-1.234567893", -1.234567893), + ("-1.34567893", -1.34567893), + ("-1.4567893", -1.4567893), + ("-1.567893", -1.567893), + ("-1.67893", -1.67893), + ("-1.7893", -1.7893), + ("-1.893", -1.893), + ("-1.3", -1.3), + ("1.1234567893", 1.1234567893), + ("1.234567893", 1.234567893), + ("1.34567893", 1.34567893), + ("1.4567893", 1.4567893), + ("1.567893", 1.567893), + ("1.67893", 1.67893), + ("1.7893", 1.7893), + ("1.893", 1.893), + ("1.3", 1.3), + ("true", True), + ("false", False), + ("null", None), + (" [ true, false,null] ", [True, False, None]), + ], +) +def test_loads(test_input, expected): + assert ujson.loads(test_input) == expected + + +def test_reject_bytes_default(): + data = {"a": b"b"} + with pytest.raises(TypeError): + ujson.dumps(data) + + +def test_reject_bytes_true(): + data = {"a": b"b"} + with pytest.raises(TypeError): + ujson.dumps(data, reject_bytes=True) + + +def test_reject_bytes_false(): + data = {"a": b"b"} + assert ujson.dumps(data, reject_bytes=False) == '{"a":"b"}' + + +@pytest.mark.parametrize( + "value", + [ + [b"hello"], # bytes inside a list + {"a": {"b": [b"deep"]}}, # bytes deeply nested + ], +) +def test_reject_bytes_nested(value): + # reject_bytes=True (default) applies at any nesting depth. + with pytest.raises(TypeError, match="reject_bytes is on and"): + ujson.dumps(value) + + +@pytest.mark.parametrize( + "codepoint", + [0x0, 0x7F, 0x80, 0x7FF, 0x800, 0xFFFF, 0x10000, 0x10FFFF], +) +def test_reject_bytes_false_codepoint_boundaries(codepoint): + char = chr(codepoint) + assert ujson.loads(ujson.dumps(char.encode(), reject_bytes=False)) == char + + +@pytest.mark.parametrize( + "value, error", + [ + # Bad start bytes + (b"\xfd", "Unsupported UTF-8 sequence length when encoding string"), + (b"\xfc:", "Unsupported UTF-8 sequence length when encoding string"), + (b"U>\xfb", "Unsupported UTF-8 sequence length when encoding string"), + (b"\\\xf8\x98\t", "Unsupported UTF-8 sequence length when encoding string"), + (b"\x9b", "'utf.8' codec can't decode byte 0x9b in position 1:"), + (b"B\x8a", "'utf.8' codec can't decode byte 0x8a in position 2:"), + # Bad continuation bytes (any non-start byte not matching 0b10xx_xxxx) + (b"\xcf\x13", "Invalid continuation byte in 2-byte UTF-8 sequence"), + (b"\xcfa", "Invalid continuation byte in 2-byte UTF-8 sequence"), + (b"\xd8\xcf\xd3", "Invalid continuation byte in 2-byte UTF-8 sequence"), + (b"\xd2\t\x8b\x84", "Invalid continuation byte in 2-byte UTF-8 sequence"), + (b"\xe2\x17\xce", "Invalid continuation byte in 3-byte UTF-8 sequence"), + (b"\xe2a\x17\xce", "Invalid continuation byte in 3-byte UTF-8 sequence"), + (b"\xe2\x17a", "Invalid continuation byte in 3-byte UTF-8 sequence"), + (b"\xe0\x9c\xc6\xde", "Invalid continuation byte in 3-byte UTF-8 sequence"), + (b"\xf0H\xce\x9b", "Invalid continuation byte in 4-byte UTF-8 sequence"), + (b"\xf0\xce4\x9b", "Invalid continuation byte in 4-byte UTF-8 sequence"), + # Truncated UTF-8 sequences + (b"\xc3", "Unterminated UTF-8 sequence when encoding string"), + (b"\x8c$\xe3", "Unterminated UTF-8 sequence when encoding string"), + (b"\x8c\xe3$", "Unterminated UTF-8 sequence when encoding string"), + (b"=\x8c\xe36", "Unterminated UTF-8 sequence when encoding string"), + (b"\x08\x11\xe3", "Unterminated UTF-8 sequence when encoding string"), + (b"\xf0\x90\x94", "Unterminated UTF-8 sequence when encoding string"), + # Small codepoints using longer byte sequences than they need + (b"\xc0\xa2", "Overlong 2-byte UTF-8 sequence"), + (b"A\xc1\x9c", "Overlong 2-byte UTF-8 sequence"), + (b"\xc1\xbf", "Overlong 2-byte UTF-8 sequence"), + (b"N\xc0\xb4\xb4", "Overlong 2-byte UTF-8 sequence"), + (b"\xe0\x9d\xb3", "Overlong 3-byte UTF-8 sequence"), + (b"E\xe0\x9e\x8b", "Overlong 3-byte UTF-8 sequence"), + (b"\xe0\x9f\xbf", "Overlong 3-byte UTF-8 sequence"), + (b"\xf0\x80\x80\x80", "Overlong 4-byte UTF-8 sequence"), + (b"\xf0\x8f\xbf\xbf", "Overlong 4-byte UTF-8 sequence"), + (b"\xf0\x85\xa7\xbd", "Overlong 4-byte UTF-8 sequence"), + # Codepoints above unicode max + (b"\xf4\x90\x80\x80", r">U\+10FFFF in 4-byte UTF-8 sequence"), + (b"\xf7\x8f\x99\x90", r">U\+10FFFF in 4-byte UTF-8 sequence"), + (b"\xf7\xbf\xbf\xbf", r">U\+10FFFF in 4-byte UTF-8 sequence"), + ], +) +def test_dump_bytes_invalid_utf8(value, error): + with pytest.raises((OverflowError, UnicodeDecodeError), match=error): + ujson.dumps(bytes(value), reject_bytes=False) + + +def test_dump_bytes_fuzz(): + # ujson.dumps(..., reject_bytes=False) should accept or reject the same byte + # sequences as b"...".decode() when unpaired surrogates are allowed + for seed in range(10000): + r = random.Random(seed) + a = r.randbytes(r.randrange(8)) + try: + expected = a.decode(errors="surrogatepass") + except UnicodeDecodeError: + with pytest.raises((UnicodeDecodeError, OverflowError)): + ujson.dumps(a, reject_bytes=False) + else: + actual = ujson.loads(ujson.dumps(a, reject_bytes=False)) + assert actual == expected, (a, [bin(i) for i in a], actual, expected) + + +def test_encode_special_keys(): + data = {None: 0, True: 1, False: 2} + assert ujson.dumps(data) == '{"null":0,"true":1,"false":2}' + data = {None: 0} + assert ujson.dumps(data, sort_keys=True) == '{"null":0}' + data = {True: 1, False: 2} + assert ujson.dumps(data, sort_keys=True) == '{"false":2,"true":1}' + + +class TestDefaultFunction: + iso8601_time_format = "%Y-%m-%dT%H:%M:%S.%f" + + class CustomObject: + pass + + class UnjsonableObject: + pass + + @classmethod + def default(cls, value): + if isinstance(value, dt.datetime): + return value.strftime(cls.iso8601_time_format) + elif isinstance(value, uuid.UUID): + return value.hex + elif isinstance(value, cls.CustomObject): + raise ValueError("invalid value") + return value + + def test_datetime(self): + now = dt.datetime.now() + expected_output = '"%s"' % now.strftime(self.iso8601_time_format) + assert ujson.dumps(now, default=self.default) == expected_output + + def test_uuid(self): + uuid4 = uuid.uuid4() + expected_output = '"%s"' % uuid4.hex + assert ujson.dumps(uuid4, default=self.default) == expected_output + + def test_exception_in_default(self): + custom_obj = self.CustomObject() + with pytest.raises(ValueError, match="invalid value"): + ujson.dumps(custom_obj, default=self.default) + + def test_exception_type_preserved(self): + # Any exception type raised by default= must propagate unchanged. + class Boom(Exception): + pass + + with pytest.raises(Boom): + ujson.dumps(object(), default=lambda o: (_ for _ in ()).throw(Boom())) + + @pytest.mark.skip_leak_test # Known memory leak + def test_recursive_default(self): + unjsonable_obj = self.UnjsonableObject() + with pytest.raises(TypeError, match="maximum recursion depth exceeded"): + ujson.dumps(unjsonable_obj, default=self.default) + + +@pytest.mark.parametrize("indent", [999, 1000, 1001, 1 << 30, 1 << 63, 1 << 128]) +def test_dump_huge_indent(indent): + obj = {"list": [1, [2, 3], 4], "nested": {"key": "value", "a": True}} + if indent <= 1000: + assert ujson.loads(ujson.encode(obj, indent=indent)) == obj + else: + with pytest.raises((ValueError, OverflowError)): + ujson.encode(obj, indent=indent) + + +def test_negative_indent(): + obj = {"a": [1, 2], "b": "c"} + assert ujson.dumps(obj) == '{"a":[1,2],"b":"c"}' + assert ujson.dumps(obj, 0) == '{"a":[1,2],"b":"c"}' + assert ujson.dumps(obj, indent=-1) == '{"a": [1,2],"b": "c"}' + assert ujson.dumps(obj, indent=-1000000) == '{"a": [1,2],"b": "c"}' + assert ( + ujson.dumps(obj, indent=2) == '{\n "a": [\n 1,\n 2\n ],\n "b": "c"\n}' + ) + + +@pytest.mark.parametrize("first_length", list(range(2, 7))) +@pytest.mark.parametrize("second_length", list(range(10919, 10924))) +def test_dump_long_string(first_length, second_length): + ujson.dumps(["a" * first_length, "\x00" * second_length]) + + +def test_dump_indented_nested_list(): + a = _a = [] + for i in range(20): + _a.append(list(range(i))) + _a = _a[-1] + ujson.dumps(a, indent=i) + + +@pytest.mark.parametrize("indent", [0, 1, 2, 4, 5, 8, 49]) +def test_issue_334(indent): + path = Path(__file__).with_name("334-reproducer.json") + a = ujson.loads(path.read_bytes()) + ujson.dumps(a, indent=indent) + + +@pytest.mark.skipif( + sys.implementation.name in ("pypy", "graalpy"), + reason="PyPy & GraalPy use incompatible GC", +) +@pytest.mark.skip_leak_test # Does its own leak checking +def test_default_ref_counting(): + class DefaultRefCountingClass: + def __init__(self, value): + self._value = value + + def convert(self): + if self._value > 1: + return type(self)(self._value - 1) + return 0 + + import gc + + gc.collect() + ujson.dumps(DefaultRefCountingClass(3), default=lambda x: x.convert()) + assert not any( + type(o).__name__ == "DefaultRefCountingClass" for o in gc.get_objects() + ) + + +@pytest.mark.skipif( + sys.implementation.name == "graalpy", + reason="GraalPy has an incompatible stub implementation of getrefcount", +) +@pytest.mark.parametrize("sort_keys", [False, True]) +@pytest.mark.skip_leak_test # Does its own leak detection +def test_obj_str_exception(sort_keys): + class Obj: + def __str__(self): + raise NotImplementedError + + key = Obj() + getrefcount = getattr(sys, "getrefcount", lambda x: 0) + old = getrefcount(key) + with pytest.raises(NotImplementedError): + ujson.dumps({key: 1}, sort_keys=sort_keys) + assert getrefcount(key) == old + + +@pytest.mark.skipif( + sys.implementation.name == "graalpy", reason="GraalPy does not support tracemalloc" +) +def no_memory_leak(func_code, n=None): + code = f"import functools, ujson; func = {func_code}" + path = os.path.join(os.path.dirname(__file__), "memory.py") + n = [str(n)] if n is not None else [] + p = subprocess.run([sys.executable, path, code] + n) + assert p.returncode == 0 + + +@pytest.mark.skipif( + sys.implementation.name == "graalpy", reason="GraalPy does not support tracemalloc" +) +def test_no_memory_leak_default_ascii(): + rng = random.Random(631) + value = "ascii: " + "".join(rng.choice(string.ascii_letters) for _ in range(32)) + data = {"x": object()} + + ujson.dumps(data, ensure_ascii=True, default=lambda o: value) + + +@pytest.mark.skipif( + sys.implementation.name == "graalpy", reason="GraalPy does not support tracemalloc" +) +def test_no_memory_leak_default_non_ascii(): + rng = random.Random(631) + non_ascii_chars = "生日快乐中文测试汉字" + non_ascii_value = "non_ascii: " + "".join( + rng.choice(non_ascii_chars) for _ in range(8) + ) + data = {"x": object()} + + ujson.dumps(data, ensure_ascii=True, default=lambda o: non_ascii_value) + + +@pytest.mark.skipif( + sys.implementation.name in ("pypy", "graalpy"), + reason="PyPy & GraalPy use incompatible GC", +) +@pytest.mark.parametrize("input", ['["a" * 11000, b""]']) +def test_no_memory_leak_encoding_errors(input): + no_memory_leak(f"functools.partial(ujson.dumps, {input})") + + +@pytest.mark.parametrize( + "separators, expected", + [ + (None, '{"a":0,"b":1}'), + ((",", ":"), '{"a":0,"b":1}'), + ((", ", ": "), '{"a": 0, "b": 1}'), + # And some weird values, even though they produce invalid JSON + (("\u203d", "\u00a1"), '{"a"\u00a10\u203d"b"\u00a11}'), + (("i\x00", "k\x00"), '{"a"k\x000i\x00"b"k\x001}'), + (("\udc80", "\udc81"), '{"a"\udc810\udc80"b"\udc811}'), + ], +) +def test_separators(separators, expected): + assert ujson.dumps({"a": 0, "b": 1}, separators=separators) == expected + + +@pytest.mark.parametrize("value", [{}, []]) +def test_separators_empty_collection(value): + # Custom separators do not affect the compact form of empty collections. + assert ujson.dumps(value, separators=(" , ", " : ")) == ujson.dumps(value) + + +def test_separators_with_indent_roundtrip(): + data = {"x": [1, 2], "y": 3} + result = ujson.dumps(data, indent=2, separators=(",", ": ")) + assert ujson.loads(result) == data + + +@pytest.mark.parametrize( + "separators, expected_exception", + [ + (True, TypeError), + (0, TypeError), + (b"", TypeError), + ((), ValueError), + ((",",), ValueError), + ((",", ":", "x"), ValueError), + ((True, 0), TypeError), + ((",", True), TypeError), + ((True, ":"), TypeError), + ((b",", b":"), TypeError), + ], +) +def test_separators_errors(separators, expected_exception): + with pytest.raises(expected_exception): + ujson.dumps({"a": 0, "b": 1}, separators=separators) + + +def test_loads_bytes_like(): + assert ujson.loads(b"123") == 123 + if hasattr(sys, "pypy_version_info"): + with pytest.raises(TypeError, match="PyPy"): + ujson.loads(memoryview(b"{}")) + else: + assert ujson.loads(memoryview(b'["a", "b", "c"]')) == ["a", "b", "c"] + assert ujson.loads(bytearray(b"99")) == 99 + assert ujson.loads('"🦄🐳"'.encode()) == "🦄🐳" + # array.array exports the C-contiguous buffer protocol and must be accepted. + import array as _array + + if not hasattr(sys, "pypy_version_info"): + assert ujson.loads(_array.array("B", b'{"a":1}')) == {"a": 1} + + +@pytest.mark.skipif( + sys.implementation.name in ("pypy", "graalpy"), + reason="PyPy & GraalPy use incompatible GC", +) +def test_loads_bytes_like_refcounting(): + import gc + + gc.collect() + buffer = b'{"a": 99}' + old = sys.getrefcount(buffer) + assert ujson.loads(buffer) == {"a": 99} + assert sys.getrefcount(buffer) == old + + buffer = b'{"a": invalid}' + old = sys.getrefcount(buffer) + with pytest.raises(ValueError): + ujson.loads(buffer) + assert sys.getrefcount(buffer) == old + + +@pytest.mark.skipif( + sys.implementation.name == "graalpy", + reason="GraalPy does not support buffer protocol for non C-contiguous buffers", +) +def test_loads_non_c_contiguous(): + buffer = memoryview(b"".join(bytes([i]) + b"_" for i in b"[1, 2, 3]"))[::2] + assert not buffer.c_contiguous + assert ujson.loads(bytes(buffer)) == [1, 2, 3] + with pytest.raises(TypeError): + ujson.loads(buffer) + + +class ExampleIntEnum(enum.IntEnum): + FOO = 42 + + +class ExampleFloatEnum(float, enum.Enum): + FOO = 3.1416 + + +@pytest.mark.parametrize( + "enum_class, expected", + [(ExampleIntEnum, "42"), (ExampleFloatEnum, "3.1416")], +) +def test_enum(enum_class, expected): + assert ujson.dumps(enum_class.FOO) == expected + + +def test_nested_json_decode_error(): + """Parsing a nested json string raises JSONDecodeError and not SystemError.""" + + # Test that nested JSON with invalid format raises JSONDecodeError + with pytest.raises(ujson.JSONDecodeError): + ujson.loads(b'{{"a":"b"}:"c"}') + + # Test with another nested JSON case + with pytest.raises(ujson.JSONDecodeError): + ujson.loads('{"a":{"b":"c"}:"d"}') + + # Test that JSONDecodeError is a subclass of ValueError + assert issubclass(ujson.JSONDecodeError, ValueError) + + +def test_comprehensive_json_fixture(): + """ + Loads comprehensive.json — a fixture that exercises every JSON value type + at multiple nesting levels + + * ujson agrees with stdlib json on the decoded Python objects. + * A ujson roundtrip (loads → dumps → loads) is lossless. + * Each JSON type decodes to the correct Python type. + * Specific deeply-nested values are reachable and correct. + + The test also exercises ujson's documented tolerances for non-standard + JSON that stdlib json rejects: + • NaN / Infinity / -Infinity as bare value tokens + • Integers with leading zeros (e.g. "01" → 1) + • Trailing-decimal floats (e.g. "1." → 1.0) + • \\/ as an escape for '/' (allowed by the spec, used in the fixture) + """ + fixture = Path(__file__).with_name("comprehensive.json") + raw = fixture.read_bytes() + + # Agreement with stdlib json + data = ujson.loads(raw) + stdlib_data = json.loads(raw) + assert data == stdlib_data + + # Roundtrip stability + assert ujson.loads(ujson.dumps(data)) == data + + # Type correctness for every JSON primitive type + nums = data["numbers"] + assert isinstance(nums["zero"], int) and nums["zero"] == 0 + assert isinstance(nums["forty_two"], int) and nums["forty_two"] == 42 + assert isinstance(nums["negative_one"], int) and nums["negative_one"] == -1 + assert isinstance(nums["pi"], float) and math.isclose( + nums["pi"], math.pi, rel_tol=1e-12 + ) + assert isinstance(nums["half"], float) and nums["half"] == 0.5 + assert isinstance(nums["small_float"], float) + + bools = data["booleans"] + assert bools["yes"] is True + assert bools["no"] is False + + assert data["null_value"] is None + + assert isinstance(data["strings"]["plain"], str) + assert data["strings"]["empty"] == "" + + # String escape sequences survived the roundtrip + escapes = data["strings"]["standard_escapes"] + assert "\t" in escapes # \t + assert "\n" in escapes # \n + assert "\r" in escapes # \r + assert "\b" in escapes # \b + assert "\f" in escapes # \f + assert "\\" in escapes # \\ + assert '"' in escapes # \" + assert "/" in escapes # \/ (solidus — optional escape, still valid) + + # Unicode strings + assert "é" in data["strings"]["unicode_latin"] + assert "中" in data["strings"]["unicode_cjk"] + assert "😀" in data["strings"]["unicode_emoji"] + + # All array variants + arrs = data["arrays"] + assert arrs["empty"] == [] + assert arrs["single"] == [42] + assert arrs["integers"] == list(range(1, 11)) + assert arrs["mixed"][0] == 1 + assert arrs["mixed"][1] == "two" + assert arrs["mixed"][4] is False + assert arrs["mixed"][5] is None + assert arrs["mixed"][6] == [] + assert arrs["mixed"][7] == {} + assert arrs["nested_4"][0][0][0] == [1, 2] # 4 levels deep + assert arrs["of_objects"][1]["val"] == "b" + + # All object variants + objs = data["objects"] + assert objs["empty"] == {} + assert objs["single_key"] == {"only": "value"} + assert objs["flat"]["d"] is None + + deep = objs["nested_6"]["l1"]["l2"]["l3"]["l4"]["l5"]["l6"] + assert deep == "six levels deep" + assert objs["nested_6"]["l1"]["l2"]["l3"]["l4"]["l5"]["l6_int"] == 6 + assert objs["nested_6"]["l1"]["l2"]["l3"]["l4"]["l5"]["l6_arr"] == [6, 6, 6] + + # Heavily-nested records (objects inside arrays inside objects) + alice = data["records"][0] + assert alice["name"] == "Alice" + assert alice["active"] is True + assert alice["score"] == 98.5 + assert alice["tags"] == ["admin", "superuser", "reviewer"] + assert alice["address"]["coords"]["lat"] == 39.7817 + assert alice["prefs"]["limits"]["rate"] == 10.5 + assert alice["history"][1]["action"] == "upload" + assert alice["metadata"] is None + + bob = data["records"][1] + assert bob["active"] is False + assert bob["tags"] == [] + assert bob["metadata"]["flags"] == [1, 2, 4] + + # Matrix (array of arrays of ints) + assert data["matrix"][3][4] == 20 # bottom-right corner + assert sum(data["matrix"][0]) == 15 # first row + + # Config subtree + cfg = data["config"] + assert cfg["features"]["beta"]["suboptions"]["backoff"] == [1.0, 2.0, 4.0, 8.0] + assert cfg["limits"]["burst"] is None + assert "example.com" in cfg["allowed_origins"][0] + + # ujson-specific tolerances (non-standard JSON that ujson accepts) + nan_doc = '{"values": [NaN, Infinity, -Infinity]}' + tol = ujson.loads(nan_doc) + assert math.isnan(tol["values"][0]) + assert math.isinf(tol["values"][1]) and tol["values"][1] > 0 + assert math.isinf(tol["values"][2]) and tol["values"][2] < 0 + # Both ujson and stdlib json agree. + stdlib_tol = json.loads(nan_doc) + assert math.isnan(stdlib_tol["values"][0]) + assert tol["values"][1] == stdlib_tol["values"][1] + assert tol["values"][2] == stdlib_tol["values"][2] + + # Leading-zero integer: "01" → 1 (RFC 8259 §6 violation). + # stdlib json has always rejected this; ujson is permissive. + with pytest.raises(json.JSONDecodeError): + json.loads("01") + assert ujson.loads("01") == 1 + + # Trailing-decimal float: "1." → 1.0 (not valid JSON, but ujson tolerates it). + with pytest.raises(json.JSONDecodeError): + json.loads("1.") + assert ujson.loads("1.") == 1.0 + + +def test_gil_not_reenabled(): + try: + gil_enabled = sys._is_gil_enabled() + gil_expected = bool(sys.flags.gil) + except AttributeError: + pytest.skip(reason="Free threading properties unavailable") + assert gil_enabled is gil_expected + + +""" +The following checks are not part of the standard test suite. +They can be run manually as follows: +python -c 'from tests.test_ujson import check_foo; check_foo()' +""" + + +def check_decode_decimal_no_int_overflow(): + # Requires enough free RAM to hold a ~4GB string in memory + decoded = ujson.decode(r'[0.123456789,"{}"]'.format("a" * (2**32 - 5))) + assert decoded[0] == 0.123456789 + + +""" +def test_decode_numeric_int_frc_overflow(): +input = "X.Y" +raise NotImplementedError("Implement this test!") + + +def test_decode_string_unicode_escape(): +input = "\u3131" +raise NotImplementedError("Implement this test!") + +def test_decode_string_unicode_broken_escape(): +input = "\u3131" +raise NotImplementedError("Implement this test!") + +def test_decode_string_unicode_invalid_escape(): +input = "\u3131" +raise NotImplementedError("Implement this test!") + +def test_decode_string_utf8(): +input = "someutfcharacters" +raise NotImplementedError("Implement this test!") + +""" + +""" +# Use this to look for memory leaks +if __name__ == '__main__': + import unittest + from guppy import hpy + hp = hpy() + hp.setrelheap() + while True: + try: + unittest.main() + except SystemExit: + pass + heap = hp.heapu() + print(heap) +""" diff --git a/tests/tests.py b/tests/tests.py deleted file mode 100644 index 44f813cd..00000000 --- a/tests/tests.py +++ /dev/null @@ -1,1110 +0,0 @@ - -try: - import unittest2 as unittest -except ImportError: - import unittest - -import ujson -try: - import json -except ImportError: - import simplejson as json -import math -import platform -import sys -import time -import datetime -import calendar - -try: - from StringIO import StringIO -except: - from io import StringIO - -import re -import random -import decimal -from functools import partial - -PY3 = (sys.version_info[0] >= 3) -if PY3: - xrange = range - -def _python_ver(skip_major, skip_minor=None): - major, minor = sys.version_info[:2] - return major == skip_major and (skip_minor is None or minor == skip_minor) - -json_unicode = (json.dumps if sys.version_info[0] >= 3 - else partial(json.dumps, encoding="utf-8")) - -class UltraJSONTests(unittest.TestCase): - - def test_encodeDecimal(self): - sut = decimal.Decimal("1337.1337") - encoded = ujson.encode(sut, double_precision=100) - decoded = ujson.decode(encoded) - self.assertEqual(decoded, 1337.1337) - - def test_encodeStringConversion(self): - input = "A string \\ / \b \f \n \r \t &" - not_html_encoded = '"A string \\\\ \\/ \\b \\f \\n \\r \\t <\\/script> &"' - html_encoded = '"A string \\\\ \\/ \\b \\f \\n \\r \\t \\u003c\\/script\\u003e \\u0026"' - not_slashes_escaped = input - - def helper(expected_output, **encode_kwargs): - output = ujson.encode(input, **encode_kwargs) - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, expected_output) - self.assertEqual(input, ujson.decode(output)) - - # Default behavior assumes encode_html_chars=False. - helper(not_html_encoded, ensure_ascii=True) - helper(not_html_encoded, ensure_ascii=False) - - # Make sure explicit encode_html_chars=False works. - helper(not_html_encoded, ensure_ascii=True, encode_html_chars=False) - helper(not_html_encoded, ensure_ascii=False, encode_html_chars=False) - - # Make sure explicit encode_html_chars=True does the encoding. - helper(html_encoded, ensure_ascii=True, encode_html_chars=True) - helper(html_encoded, ensure_ascii=False, encode_html_chars=True) - - # Do escape forward slashes if disabled. - helper(not_slashes_escaped, escape_forward_slashes=False) - - def testWriteEscapedString(self): - self.assertEqual('"\\u003cimg src=\'\\u0026amp;\'\\/\\u003e"', ujson.dumps("", encode_html_chars=True)) - - def test_doubleLongIssue(self): - sut = {u'a': -4342969734183514} - encoded = json.dumps(sut) - decoded = json.loads(encoded) - self.assertEqual(sut, decoded) - encoded = ujson.encode(sut, double_precision=100) - decoded = ujson.decode(encoded) - self.assertEqual(sut, decoded) - - def test_doubleLongDecimalIssue(self): - sut = {u'a': -12345678901234.56789012} - encoded = json.dumps(sut) - decoded = json.loads(encoded) - self.assertEqual(sut, decoded) - encoded = ujson.encode(sut, double_precision=100) - decoded = ujson.decode(encoded) - self.assertEqual(sut, decoded) - - def test_encodeDecodeLongDecimal(self): - sut = {u'a': -528656961.4399388} - encoded = ujson.dumps(sut, double_precision=15) - ujson.decode(encoded) - - def test_decimalDecodeTest(self): - sut = {u'a': 4.56} - encoded = ujson.encode(sut) - decoded = ujson.decode(encoded) - self.assertNotEqual(sut, decoded) - - def test_decimalDecodeTestPrecise(self): - sut = {u'a': 4.56} - encoded = ujson.encode(sut) - decoded = ujson.decode(encoded, precise_float=True) - self.assertEqual(sut, decoded) - - def test_encodeDictWithUnicodeKeys(self): - input = { u"key1": u"value1", u"key1": u"value1", u"key1": u"value1", u"key1": u"value1", u"key1": u"value1", u"key1": u"value1" } - output = ujson.encode(input) - - input = { u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1" } - output = ujson.encode(input) - - def test_encodeDoubleConversion(self): - input = math.pi - output = ujson.encode(input) - self.assertEqual(round(input, 5), round(json.loads(output), 5)) - self.assertEqual(round(input, 5), round(ujson.decode(output), 5)) - - def test_encodeWithDecimal(self): - input = 1.0 - output = ujson.encode(input) - self.assertEqual(output, "1.0") - - def test_encodeDoubleNegConversion(self): - input = -math.pi - output = ujson.encode(input) - - self.assertEqual(round(input, 5), round(json.loads(output), 5)) - self.assertEqual(round(input, 5), round(ujson.decode(output), 5)) - - def test_encodeArrayOfNestedArrays(self): - input = [[[[]]]] * 20 - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - #self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeArrayOfDoubles(self): - input = [ 31337.31337, 31337.31337, 31337.31337, 31337.31337] * 10 - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - #self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - def test_doublePrecisionTest(self): - input = 30.012345678901234 - output = ujson.encode(input, double_precision = 15) - self.assertEqual(input, json.loads(output)) - self.assertEqual(input, ujson.decode(output)) - - output = ujson.encode(input, double_precision = 9) - self.assertEqual(round(input, 9), json.loads(output)) - self.assertEqual(round(input, 9), ujson.decode(output)) - - output = ujson.encode(input, double_precision = 3) - self.assertEqual(round(input, 3), json.loads(output)) - self.assertEqual(round(input, 3), ujson.decode(output)) - - def test_invalidDoublePrecision(self): - input = 30.12345678901234567890 - output = ujson.encode(input, double_precision = 20) - # should snap to the max, which is 15 - self.assertEqual(round(input, 15), json.loads(output)) - self.assertEqual(round(input, 15), ujson.decode(output)) - - output = ujson.encode(input, double_precision = -1) - # also should snap to the max, which is 15 - self.assertEqual(round(input, 15), json.loads(output)) - self.assertEqual(round(input, 15), ujson.decode(output)) - - # will throw typeError - self.assertRaises(TypeError, ujson.encode, input, double_precision = '9') - # will throw typeError - self.assertRaises(TypeError, ujson.encode, input, double_precision = None) - - def test_encodeStringConversion(self): - input = "A string \\ / \b \f \n \r \t" - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, '"A string \\\\ \\/ \\b \\f \\n \\r \\t"') - self.assertEqual(input, ujson.decode(output)) - - def test_decodeUnicodeConversion(self): - pass - - def test_encodeUnicodeConversion1(self): - input = "Räksmörgås اسامة بن محمد بن عوض بن لادن" - enc = ujson.encode(input) - dec = ujson.decode(enc) - self.assertEqual(enc, json_unicode(input)) - self.assertEqual(dec, json.loads(enc)) - - def test_encodeControlEscaping(self): - input = "\x19" - enc = ujson.encode(input) - dec = ujson.decode(enc) - self.assertEqual(input, dec) - self.assertEqual(enc, json_unicode(input)) - - def test_encodeUnicodeConversion2(self): - input = "\xe6\x97\xa5\xd1\x88" - enc = ujson.encode(input) - dec = ujson.decode(enc) - self.assertEqual(enc, json_unicode(input)) - self.assertEqual(dec, json.loads(enc)) - - def test_encodeUnicodeSurrogatePair(self): - input = "\xf0\x90\x8d\x86" - enc = ujson.encode(input) - dec = ujson.decode(enc) - - self.assertEqual(enc, json_unicode(input)) - self.assertEqual(dec, json.loads(enc)) - - def test_encodeUnicode4BytesUTF8(self): - input = "\xf0\x91\x80\xb0TRAILINGNORMAL" - enc = ujson.encode(input) - dec = ujson.decode(enc) - - self.assertEqual(enc, json_unicode(input)) - self.assertEqual(dec, json.loads(enc)) - - def test_encodeUnicode4BytesUTF8Highest(self): - input = "\xf3\xbf\xbf\xbfTRAILINGNORMAL" - enc = ujson.encode(input) - dec = ujson.decode(enc) - - self.assertEqual(enc, json_unicode(input)) - self.assertEqual(dec, json.loads(enc)) - - # Characters outside of Basic Multilingual Plane(larger than - # 16 bits) are represented as \UXXXXXXXX in python but should be encoded - # as \uXXXX\uXXXX in json. - def testEncodeUnicodeBMP(self): - s = u'\U0001f42e\U0001f42e\U0001F42D\U0001F42D' # 🐮🐮🐭🐭 - encoded = ujson.dumps(s) - encoded_json = json.dumps(s) - - if len(s) == 4: - self.assertEqual(len(encoded), len(s) * 12 + 2) - else: - self.assertEqual(len(encoded), len(s) * 6 + 2) - - self.assertEqual(encoded, encoded_json) - decoded = ujson.loads(encoded) - self.assertEqual(s, decoded) - - # ujson outputs an UTF-8 encoded str object - if PY3: - encoded = ujson.dumps(s, ensure_ascii=False) - else: - encoded = ujson.dumps(s, ensure_ascii=False).decode("utf-8") - - # json outputs an unicode object - encoded_json = json.dumps(s, ensure_ascii=False) - self.assertEqual(len(encoded), len(s) + 2) # original length + quotes - self.assertEqual(encoded, encoded_json) - decoded = ujson.loads(encoded) - self.assertEqual(s, decoded) - - def testEncodeSymbols(self): - s = u'\u273f\u2661\u273f' # ✿♡✿ - encoded = ujson.dumps(s) - encoded_json = json.dumps(s) - self.assertEqual(len(encoded), len(s) * 6 + 2) # 6 characters + quotes - self.assertEqual(encoded, encoded_json) - decoded = ujson.loads(encoded) - self.assertEqual(s, decoded) - - # ujson outputs an UTF-8 encoded str object - if PY3: - encoded = ujson.dumps(s, ensure_ascii=False) - else: - encoded = ujson.dumps(s, ensure_ascii=False).decode("utf-8") - - # json outputs an unicode object - encoded_json = json.dumps(s, ensure_ascii=False) - self.assertEqual(len(encoded), len(s) + 2) # original length + quotes - self.assertEqual(encoded, encoded_json) - decoded = ujson.loads(encoded) - self.assertEqual(s, decoded) - - def test_encodeArrayInArray(self): - input = [[[[]]]] - output = ujson.encode(input) - - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeIntConversion(self): - input = 31337 - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeIntNegConversion(self): - input = -31337 - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeLongNegConversion(self): - input = -9223372036854775808 - output = ujson.encode(input) - - outputjson = json.loads(output) - outputujson = ujson.decode(output) - - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeListConversion(self): - input = [ 1, 2, 3, 4 ] - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeDictConversion(self): - input = { "k1": 1, "k2": 2, "k3": 3, "k4": 4 } - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(input, ujson.decode(output)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeNoneConversion(self): - input = None - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeTrueConversion(self): - input = True - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeFalseConversion(self): - input = False - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeDatetimeConversion(self): - ts = time.time() - input = datetime.datetime.fromtimestamp(ts) - output = ujson.encode(input) - expected = calendar.timegm(input.utctimetuple()) - self.assertEqual(int(expected), json.loads(output)) - self.assertEqual(int(expected), ujson.decode(output)) - - def test_encodeDateConversion(self): - ts = time.time() - input = datetime.date.fromtimestamp(ts) - - output = ujson.encode(input) - tup = ( input.year, input.month, input.day, 0, 0, 0 ) - - expected = calendar.timegm(tup) - self.assertEqual(int(expected), json.loads(output)) - self.assertEqual(int(expected), ujson.decode(output)) - - def test_encodeToUTF8(self): - input = "\xe6\x97\xa5\xd1\x88" - enc = ujson.encode(input, ensure_ascii=False) - dec = ujson.decode(enc) - self.assertEqual(enc, json_unicode(input, ensure_ascii=False)) - self.assertEqual(dec, json.loads(enc)) - - def test_decodeFromUnicode(self): - input = u"{\"obj\": 31337}" - dec1 = ujson.decode(input) - dec2 = ujson.decode(str(input)) - self.assertEqual(dec1, dec2) - - def test_encodeRecursionMax(self): - # 8 is the max recursion depth - - class O2: - member = 0 - pass - - class O1: - member = 0 - pass - - input = O1() - input.member = O2() - input.member.member = input - - try: - output = ujson.encode(input) - assert False, "Expected overflow exception" - except(OverflowError): - pass - - def test_encodeDoubleNan(self): - input = float('nan') - try: - ujson.encode(input) - assert False, "Expected exception!" - except(OverflowError): - return - assert False, "Wrong exception" - - def test_encodeDoubleInf(self): - input = float('inf') - try: - ujson.encode(input) - assert False, "Expected exception!" - except(OverflowError): - return - assert False, "Wrong exception" - - def test_encodeDoubleNegInf(self): - input = -float('inf') - try: - ujson.encode(input) - assert False, "Expected exception!" - except(OverflowError): - return - assert False, "Wrong exception" - - - def test_decodeJibberish(self): - input = "fdsa sda v9sa fdsa" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeBrokenArrayStart(self): - input = "[" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeBrokenObjectStart(self): - input = "{" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeBrokenArrayEnd(self): - input = "]" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeArrayDepthTooBig(self): - input = '[' * (1024 * 1024) - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeBrokenObjectEnd(self): - input = "}" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeObjectDepthTooBig(self): - input = '{' * (1024 * 1024) - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeStringUnterminated(self): - input = "\"TESTING" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeStringUntermEscapeSequence(self): - input = "\"TESTING\\\"" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeStringBadEscape(self): - input = "\"TESTING\\\"" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeTrueBroken(self): - input = "tru" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeFalseBroken(self): - input = "fa" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - def test_decodeNullBroken(self): - input = "n" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - assert False, "Wrong exception" - - - def test_decodeBrokenDictKeyTypeLeakTest(self): - input = '{{1337:""}}' - for x in xrange(1000): - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError) as e: - continue - - assert False, "Wrong exception" - - def test_decodeBrokenDictLeakTest(self): - input = '{{"key":"}' - for x in xrange(1000): - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - continue - - assert False, "Wrong exception" - - def test_decodeBrokenListLeakTest(self): - input = '[[[true' - for x in xrange(1000): - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - continue - - assert False, "Wrong exception" - - def test_decodeDictWithNoKey(self): - input = "{{{{31337}}}}" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - - assert False, "Wrong exception" - - def test_decodeDictWithNoColonOrValue(self): - input = "{{{{\"key\"}}}}" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - - assert False, "Wrong exception" - - def test_decodeDictWithNoValue(self): - input = "{{{{\"key\":}}}}" - try: - ujson.decode(input) - assert False, "Expected exception!" - except(ValueError): - return - - assert False, "Wrong exception" - - def test_decodeNumericIntPos(self): - input = "31337" - self.assertEqual (31337, ujson.decode(input)) - - def test_decodeNumericIntNeg(self): - input = "-31337" - self.assertEqual (-31337, ujson.decode(input)) - - def test_encodeUnicode4BytesUTF8Fail(self): - input = b"\xfd\xbf\xbf\xbf\xbf\xbf" - try: - enc = ujson.encode(input) - assert False, "Expected exception" - except OverflowError: - pass - - def test_encodeNullCharacter(self): - input = "31337 \x00 1337" - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - input = "\x00" - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - self.assertEqual('" \\u0000\\r\\n "', ujson.dumps(u" \u0000\r\n ")) - - def test_decodeNullCharacter(self): - input = "\"31337 \\u0000 31337\"" - self.assertEqual(ujson.decode(input), json.loads(input)) - - def test_encodeListLongConversion(self): - input = [9223372036854775807, 9223372036854775807, 9223372036854775807, - 9223372036854775807, 9223372036854775807, 9223372036854775807 ] - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeListLongUnsignedConversion(self): - input = [18446744073709551615, 18446744073709551615, 18446744073709551615] - output = ujson.encode(input) - - self.assertEquals(input, json.loads(output)) - self.assertEquals(input, ujson.decode(output)) - - def test_encodeLongConversion(self): - input = 9223372036854775807 - output = ujson.encode(input) - self.assertEqual(input, json.loads(output)) - self.assertEqual(output, json.dumps(input)) - self.assertEqual(input, ujson.decode(output)) - - def test_encodeLongUnsignedConversion(self): - input = 18446744073709551615 - output = ujson.encode(input) - - self.assertEquals(input, json.loads(output)) - self.assertEquals(output, json.dumps(input)) - self.assertEquals(input, ujson.decode(output)) - - def test_numericIntExp(self): - input = "1337E40" - output = ujson.decode(input) - self.assertEqual(output, json.loads(input)) - - def test_numericIntFrcExp(self): - input = "1.337E40" - output = ujson.decode(input) - self.assertEqual(output, json.loads(input)) - - def test_decodeNumericIntExpEPLUS(self): - input = "1337E+9" - output = ujson.decode(input) - self.assertEqual(output, json.loads(input)) - - def test_decodeNumericIntExpePLUS(self): - input = "1.337e+40" - output = ujson.decode(input) - self.assertEqual(output, json.loads(input)) - - def test_decodeNumericIntExpE(self): - input = "1337E40" - output = ujson.decode(input) - self.assertEqual(output, json.loads(input)) - - def test_decodeNumericIntExpe(self): - input = "1337e40" - output = ujson.decode(input) - self.assertEqual(output, json.loads(input)) - - def test_decodeNumericIntExpEMinus(self): - input = "1.337E-4" - output = ujson.decode(input) - self.assertEqual(output, json.loads(input)) - - def test_decodeNumericIntExpeMinus(self): - input = "1.337e-4" - output = ujson.decode(input) - self.assertEqual(output, json.loads(input)) - - def test_dumpToFile(self): - f = StringIO() - ujson.dump([1, 2, 3], f) - self.assertEqual("[1,2,3]", f.getvalue()) - - def test_dumpToFileLikeObject(self): - class filelike: - def __init__(self): - self.bytes = '' - def write(self, bytes): - self.bytes += bytes - f = filelike() - ujson.dump([1, 2, 3], f) - self.assertEqual("[1,2,3]", f.bytes) - - def test_dumpFileArgsError(self): - try: - ujson.dump([], '') - except TypeError: - pass - else: - assert False, 'expected TypeError' - - def test_loadFile(self): - f = StringIO("[1,2,3,4]") - self.assertEqual([1, 2, 3, 4], ujson.load(f)) - - def test_loadFileLikeObject(self): - class filelike: - def read(self): - try: - self.end - except AttributeError: - self.end = True - return "[1,2,3,4]" - f = filelike() - self.assertEqual([1, 2, 3, 4], ujson.load(f)) - - def test_loadFileArgsError(self): - try: - ujson.load("[]") - except TypeError: - pass - else: - assert False, "expected TypeError" - - def test_version(self): - assert re.match(r'^\d+\.\d+(\.\d+)?$', ujson.__version__), \ - "ujson.__version__ must be a string like '1.4.0'" - - def test_encodeNumericOverflow(self): - try: - ujson.encode(12839128391289382193812939) - except OverflowError: - pass - else: - assert False, "expected OverflowError" - - def test_encodeNumericOverflowNested(self): - for n in xrange(0, 100): - class Nested: - x = 12839128391289382193812939 - - nested = Nested() - - try: - ujson.encode(nested) - except OverflowError: - pass - else: - assert False, "expected OverflowError" - - def test_decodeNumberWith32bitSignBit(self): - #Test that numbers that fit within 32 bits but would have the - # sign bit set (2**31 <= x < 2**32) are decoded properly. - boundary1 = 2**31 - boundary2 = 2**32 - docs = ( - '{"id": 3590016419}', - '{"id": %s}' % 2**31, - '{"id": %s}' % 2**32, - '{"id": %s}' % ((2**32)-1), - ) - results = (3590016419, 2**31, 2**32, 2**32-1) - for doc,result in zip(docs, results): - self.assertEqual(ujson.decode(doc)['id'], result) - - def test_encodeBigEscape(self): - for x in xrange(10): - if PY3: - base = '\u00e5'.encode('utf-8') - else: - base = "\xc3\xa5" - input = base * 1024 * 1024 * 2 - output = ujson.encode(input) - - def test_decodeBigEscape(self): - for x in xrange(10): - if PY3: - base = '\u00e5'.encode('utf-8') - quote = "\"".encode() - else: - base = "\xc3\xa5" - quote = "\"" - input = quote + (base * 1024 * 1024 * 2) + quote - output = ujson.decode(input) - - def test_toDict(self): - d = {u"key": 31337} - - class DictTest: - def toDict(self): - return d - - o = DictTest() - output = ujson.encode(o) - dec = ujson.decode(output) - self.assertEqual(dec, d) - - def test_decodeArrayTrailingCommaFail(self): - input = "[31337,]" - try: - ujson.decode(input) - except ValueError: - pass - else: - assert False, "expected ValueError" - - def test_decodeArrayLeadingCommaFail(self): - input = "[,31337]" - try: - ujson.decode(input) - except ValueError: - pass - else: - assert False, "expected ValueError" - - def test_decodeArrayOnlyCommaFail(self): - input = "[,]" - try: - ujson.decode(input) - except ValueError: - pass - else: - assert False, "expected ValueError" - - def test_decodeArrayUnmatchedBracketFail(self): - input = "[]]" - try: - ujson.decode(input) - except ValueError: - pass - else: - assert False, "expected ValueError" - - def test_decodeArrayEmpty(self): - input = "[]" - obj = ujson.decode(input) - self.assertEqual([], obj) - - def test_decodeArrayDict(self): - input = "{}" - obj = ujson.decode(input) - self.assertEqual({}, obj) - - def test_decodeArrayOneItem(self): - input = "[31337]" - ujson.decode(input) - - def test_decodeLongUnsignedValue(self): - input = "18446744073709551615" - ujson.decode(input) - - def test_decodeBigValue(self): - input = "9223372036854775807" - ujson.decode(input) - - def test_decodeSmallValue(self): - input = "-9223372036854775808" - ujson.decode(input) - - def test_decodeTooBigValue(self): - try: - input = "18446744073709551616" - ujson.decode(input) - except ValueError as e: - pass - else: - assert False, "expected ValueError" - - def test_decodeTooSmallValue(self): - try: - input = "-90223372036854775809" - ujson.decode(input) - except ValueError as e: - pass - else: - assert False, "expected ValueError" - - def test_decodeVeryTooBigValue(self): - try: - input = "18446744073709551616" - ujson.decode(input) - except ValueError: - pass - else: - assert False, "expected ValueError" - - def test_decodeVeryTooSmallValue(self): - try: - input = "-90223372036854775809" - ujson.decode(input) - except ValueError: - pass - else: - assert False, "expected ValueError" - - def test_decodeWithTrailingWhitespaces(self): - input = "{}\n\t " - ujson.decode(input) - - def test_decodeWithTrailingNonWhitespaces(self): - try: - input = "{}\n\t a" - ujson.decode(input) - except ValueError: - pass - else: - assert False, "expected ValueError" - - def test_decodeArrayWithBigInt(self): - try: - ujson.loads('[18446744073709551616]') - except ValueError: - pass - else: - assert False, "expected ValueError" - - def test_decodeArrayFaultyUnicode(self): - try: - ujson.loads('[18446744073709551616]') - except ValueError: - pass - else: - assert False, "expected ValueError" - - def test_decodeFloatingPointAdditionalTests(self): - self.assertEqual(-1.1234567893, ujson.loads("-1.1234567893")) - self.assertEqual(-1.234567893, ujson.loads("-1.234567893")) - self.assertEqual(-1.34567893, ujson.loads("-1.34567893")) - self.assertEqual(-1.4567893, ujson.loads("-1.4567893")) - self.assertEqual(-1.567893, ujson.loads("-1.567893")) - self.assertEqual(-1.67893, ujson.loads("-1.67893")) - self.assertEqual(-1.7893, ujson.loads("-1.7893")) - self.assertEqual(-1.893, ujson.loads("-1.893")) - self.assertEqual(-1.3, ujson.loads("-1.3")) - - self.assertEqual(1.1234567893, ujson.loads("1.1234567893")) - self.assertEqual(1.234567893, ujson.loads("1.234567893")) - self.assertEqual(1.34567893, ujson.loads("1.34567893")) - self.assertEqual(1.4567893, ujson.loads("1.4567893")) - self.assertEqual(1.567893, ujson.loads("1.567893")) - self.assertEqual(1.67893, ujson.loads("1.67893")) - self.assertEqual(1.7893, ujson.loads("1.7893")) - self.assertEqual(1.893, ujson.loads("1.893")) - self.assertEqual(1.3, ujson.loads("1.3")) - - def test_encodeBigSet(self): - s = set() - for x in xrange(0, 100000): - s.add(x) - ujson.encode(s) - - def test_encodeBlist(self): - try: - from blist import blist - except ImportError: - return - - b = blist(range(10)) - c = ujson.dumps(b) - d = ujson.loads(c) - - self.assertEqual(10, len(d)) - - for x in xrange(10): - self.assertEqual(x, d[x]) - - def test_encodeEmptySet(self): - s = set() - self.assertEqual("[]", ujson.encode(s)) - - def test_encodeSet(self): - s = set([1,2,3,4,5,6,7,8,9]) - enc = ujson.encode(s) - dec = ujson.decode(enc) - - for v in dec: - self.assertTrue(v in s) - - def test_ReadBadObjectSyntax(self): - try: - ujson.loads('{"age", 44}') - except ValueError: - pass - else: - assert False, "expected ValueError" - - def test_ReadTrue(self): - self.assertEqual(True, ujson.loads("true")) - - def test_ReadFalse(self): - self.assertEqual(False, ujson.loads("false")) - - def test_ReadNull(self): - self.assertEqual(None, ujson.loads("null")) - - def test_WriteTrue(self): - self.assertEqual("true", ujson.dumps(True)) - - def test_WriteFalse(self): - self.assertEqual("false", ujson.dumps(False)) - - def test_WriteNull(self): - self.assertEqual("null", ujson.dumps(None)) - - def test_ReadArrayOfSymbols(self): - self.assertEqual([True, False, None], ujson.loads(" [ true, false,null] ")) - - def test_WriteArrayOfSymbolsFromList(self): - self.assertEqual("[true,false,null]", ujson.dumps([True, False, None])) - - def test_WriteArrayOfSymbolsFromTuple(self): - self.assertEqual("[true,false,null]", ujson.dumps((True, False, None))) - - @unittest.skipIf(not PY3, "Only raises on Python 3") - def test_encodingInvalidUnicodeCharacter(self): - s = "\udc7f" - self.assertRaises(UnicodeEncodeError, ujson.dumps, s) - - def test_sortKeys(self): - data = {"a": 1, "c": 1, "b": 1, "e": 1, "f": 1, "d": 1} - sortedKeys = ujson.dumps(data, sort_keys=True) - self.assertEqual(sortedKeys, '{"a":1,"b":1,"c":1,"d":1,"e":1,"f":1}') - -""" -def test_decodeNumericIntFrcOverflow(self): -input = "X.Y" -raise NotImplementedError("Implement this test!") - - -def test_decodeStringUnicodeEscape(self): -input = "\u3131" -raise NotImplementedError("Implement this test!") - -def test_decodeStringUnicodeBrokenEscape(self): -input = "\u3131" -raise NotImplementedError("Implement this test!") - -def test_decodeStringUnicodeInvalidEscape(self): -input = "\u3131" -raise NotImplementedError("Implement this test!") - -def test_decodeStringUTF8(self): -input = "someutfcharacters" -raise NotImplementedError("Implement this test!") - -""" - -if __name__ == "__main__": - unittest.main() - - -""" -# Use this to look for memory leaks -if __name__ == '__main__': - from guppy import hpy - hp = hpy() - hp.setrelheap() - while True: - try: - unittest.main() - except SystemExit: - pass - heap = hp.heapu() - print heap - -""" diff --git a/tox.ini b/tox.ini new file mode 100644 index 00000000..13bbbac4 --- /dev/null +++ b/tox.ini @@ -0,0 +1,23 @@ +[tox] +requires = + tox>=4.2 +env_list = + lint + py{py3, 314, 313, 312, 311, 310} + +[testenv] +deps = + pytest +pass_env = + FORCE_COLOR +commands = + {envpython} -m pytest {posargs} + +[testenv:lint] +skip_install = true +deps = + prek +pass_env = + PREK_COLOR +commands = + prek run --all-files --show-diff-on-failure diff --git a/ujson-stubs/__init__.pyi b/ujson-stubs/__init__.pyi new file mode 100644 index 00000000..9dc5134c --- /dev/null +++ b/ujson-stubs/__init__.pyi @@ -0,0 +1,56 @@ +from collections.abc import Callable +from typing import Any, Final + +from _typeshed import SupportsRead, SupportsWrite + +__version__: Final[str] + +def encode( + obj: Any, + ensure_ascii: bool = ..., + encode_html_chars: bool = ..., + escape_forward_slashes: bool = ..., + sort_keys: bool = ..., + indent: int = ..., + allow_nan: bool = ..., + reject_bytes: bool = ..., + default: ( + Callable[[Any], Any] | None + ) = None, # Specify how to serialize arbitrary types + separators: tuple[str, str] | None = None, +) -> str: ... +def dumps( + obj: Any, + ensure_ascii: bool = ..., + encode_html_chars: bool = ..., + escape_forward_slashes: bool = ..., + sort_keys: bool = ..., + indent: int = ..., + allow_nan: bool = ..., + reject_bytes: bool = ..., + default: ( + Callable[[Any], Any] | None + ) = None, # Specify how to serialize arbitrary types + separators: tuple[str, str] | None = None, +) -> str: ... +def dump( + obj: Any, + fp: SupportsWrite[str], + *, + ensure_ascii: bool = ..., + encode_html_chars: bool = ..., + escape_forward_slashes: bool = ..., + sort_keys: bool = ..., + indent: int = ..., + allow_nan: bool = ..., + reject_bytes: bool = ..., + default: ( + Callable[[Any], Any] | None + ) = None, # Specify how to serialize arbitrary types + separators: tuple[str, str] | None = None, +) -> None: ... +def decode(s: str | bytes | bytearray) -> Any: ... +def loads(s: str | bytes | bytearray) -> Any: ... +def load(fp: SupportsRead[str | bytes | bytearray]) -> Any: ... + +class JSONDecodeError(ValueError): ...