Skip to content

Commit

Permalink
Merge pull request #133 from skirpichev/misc
Browse files Browse the repository at this point in the history
Misc fixes
  • Loading branch information
skirpichev authored Jan 26, 2025
2 parents 485a832 + 1b4d137 commit d186dc2
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ jobs:
python-version: ${{ matrix.python-version }}
- run: |
sudo apt-get update
sudo apt-get install libgmp-dev
sudo apt-get install lcov
- run: bash scripts/cibw_before_all.sh
- run: pip install --upgrade pip setuptools setuptools_scm
- run: pip --verbose install --editable .[tests]
- run: |
- name: Build and run coverage tests
run: |
python setup.py clean
CFLAGS="-coverage" python setup.py develop
python setup.py develop
pytest --hypothesis-profile=default
lcov --capture --directory . --no-external \
--output-file build/coverage-${{ matrix.python-version }}.info
env:
CFLAGS: -coverage
LD_LIBRARY_PATH: .local/lib/
- uses: actions/upload-artifact@v4
with:
name: coverage-${{ matrix.python-version }}
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
python-version: [3.9, '3.10', pypy3.10-nightly, 3.11, 3.12, 3.13, 3.14]
runs-on: ubuntu-24.04
env:
CFLAGS: -Wpedantic -Werror -std=c17
PYTEST_ADDOPTS: --verbose
steps:
- uses: actions/checkout@v4
Expand All @@ -27,12 +26,14 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
- run: |
sudo apt-get update
sudo apt-get install libgmp-dev
- run: bash scripts/cibw_before_all.sh
- run: pip install --upgrade pip
- run: pip --verbose install --editable .[tests]
env:
CFLAGS: -Wpedantic -Werror -std=c17
- run: pytest
env:
LD_LIBRARY_PATH: .local/lib/
macos:
needs:
- linux
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ venv/
*.egg-info/
*.swp
build/
.local/
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ also few functions (factorial, gcd and isqrt), compatible with the stdlib's
module math.


Warning on alloca
-----------------

Most GMP packages enable using alloca() for temporary workspace allocation.
This module can't prevent a crash in case of a stack overflow. To avoid this,
you should compile the GMP library with '--disable-alloca' configure option to
use rather the heap for all temporary allocations.


Motivation
----------

Expand Down
1 change: 1 addition & 0 deletions scripts/cibw_before_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rm config.guess && mv configfsf.guess config.guess && chmod +x config.guess
--enable-shared \
--disable-static \
--with-pic \
--disable-alloca \
--prefix=$PREFIX -q
make -j6 -s
make -s install
Expand Down
29 changes: 13 additions & 16 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math
import platform
import resource

import pytest
from gmp import factorial, gcd, isqrt, mpz
Expand All @@ -25,22 +26,18 @@ def test_factorial(x):
reason="FIXME: setrlimit fails with ValueError on MacOS")
@pytest.mark.skipif(platform.python_implementation() == "PyPy",
reason="XXX: bug in PyNumber_ToBase()?")
def test_factorial_outofmemory():
import random
import resource

for _ in range(100):
soft, hard = resource.getrlimit(resource.RLIMIT_AS)
resource.setrlimit(resource.RLIMIT_AS, (1024*64*1024, hard))
a = random.randint(12811, 24984)
a = mpz(a)
while True:
try:
factorial(a)
a *= 2
except MemoryError:
break
resource.setrlimit(resource.RLIMIT_AS, (soft, hard))
@given(integers(min_value=12811, max_value=24984))
def test_factorial_outofmemory(x):
soft, hard = resource.getrlimit(resource.RLIMIT_AS)
resource.setrlimit(resource.RLIMIT_AS, (1024*32*1024, hard))
a = mpz(x)
while True:
try:
factorial(a)
a *= 2
except MemoryError:
break
resource.setrlimit(resource.RLIMIT_AS, (soft, hard))


@given(integers(), integers())
Expand Down
3 changes: 1 addition & 2 deletions tests/test_mpz.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import operator
import pickle
import platform
import resource
import string
import sys
import warnings
Expand Down Expand Up @@ -776,8 +777,6 @@ def test_pickle(protocol, x):
@example(249846727467293)
@example(1292734994793)
def test_outofmemory(x):
import resource

soft, hard = resource.getrlimit(resource.RLIMIT_AS)
resource.setrlimit(resource.RLIMIT_AS, (1024*32*1024, hard))
mx = mpz(x)
Expand Down

0 comments on commit d186dc2

Please sign in to comment.