Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ develop = ["python-gmp[tests]", "prek", "pyperf"]
line-length = 79

[tool.ruff.lint]
select = ["E", "F", "I", "PT", "W", "Q", "SIM"]
select = ["E", "F", "I", "PT", "W", "Q", "SIM", "B", "UP", "RUF"]

[tool.cibuildwheel]
build-frontend = {name="build", args=["--verbose", "-Csetup-args=--vsenv"]}
Expand Down
2 changes: 1 addition & 1 deletion scripts/gitversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def git_version():
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=os.path.dirname(__file__))
out, err = p.communicate()
out, _ = p.communicate()
if p.returncode:
raise RuntimeError("Non-zero return code from git-describe: "
f"{p.returncode}")
Expand Down
56 changes: 31 additions & 25 deletions tests/test_mpz.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,61 +127,65 @@ def test_format_interface():
with pytest.raises(ValueError, match="Unknown format code"):
format(mx, "q")
with pytest.raises(ValueError,
match="(Unknown format code|Invalid format specifier)"):
match=r"(Unknown format code|Invalid format)"):
format(mx, "\x81")
with pytest.raises(ValueError,
match=(r"Negative zero coercion \(z\) not allowed|"
"Invalid conversion specification")):
format(mx, "zd")
with pytest.raises(ValueError, match="Precision not allowed"):
format(mx, ".10d")
with pytest.raises(ValueError, match="Cannot specify '_' with 'n'."):
with pytest.raises(ValueError, match=r"Cannot specify '_' with 'n'."):
format(mx, "_n")
with pytest.raises(ValueError, match="Cannot specify ',' with 'n'."):
with pytest.raises(ValueError, match=r"Cannot specify ',' with 'n'."):
format(mx, ",n")
with pytest.raises(ValueError, match="Cannot specify '_' with 'c'."):
with pytest.raises(ValueError, match=r"Cannot specify '_' with 'c'."):
format(mx, "_c")
with pytest.raises(ValueError,
match=("Invalid format specifier|"
match=(r"Invalid format specifier|"
"Invalid conversion specification")):
format(mx, "f=10dx")
with pytest.raises(ValueError,
match=("Format specifier missing precision"
"|no precision given")):
r"|no precision given")):
format(mx, ".d")
with pytest.raises(ValueError, match="many decimal digits|width too big"):
with pytest.raises(ValueError, match=r"many decimal digits|width too big"):
format(mx, "f=10000000000000000000d")
with pytest.raises(ValueError, match=("many decimal digits|"
with pytest.raises(ValueError, match=(r"many decimal digits|"
"precision too big")):
format(mx, ".10000000000000000000f")
with pytest.raises(ValueError, match="Cannot specify both ',' and '_'."):
with pytest.raises(ValueError, match=r"Cannot specify both ',' and '_'."):
format(mx, ",_d")
with pytest.raises(ValueError, match="Cannot specify both ',' and '_'."):
with pytest.raises(ValueError, match=r"Cannot specify both ',' and '_'."):
format(mx, "_,d")
with pytest.raises(ValueError, match="Cannot specify ',' with 'x'."):
with pytest.raises(ValueError, match=r"Cannot specify ',' with 'x'."):
format(mx, ",x")
with pytest.raises(ValueError,
match=("Cannot specify ',' with|"
match=(r"Cannot specify ',' with|"
"Invalid format specifier")):
format(mx, ",\xa0")
with pytest.raises(ValueError, match="Sign not allowed"):
format(mx, "+c")
with pytest.raises(ValueError, match=r"Alternate form \(#\) not allowed"):
format(mx, "#c")
pytest.raises(OverflowError, lambda: format(mpz(123456789), "c"))
pytest.raises(OverflowError, lambda: format(mpz(10**100), "c"))
pytest.raises(OverflowError, lambda: format(mpz(-1), "c"))
pytest.raises(OverflowError, lambda: format(mpz(1<<32), "c"))
with pytest.raises(OverflowError):
format(mpz(123456789), "c")
with pytest.raises(OverflowError):
format(mpz(10**100), "c")
with pytest.raises(OverflowError):
format(mpz(-1), "c")
with pytest.raises(OverflowError):
format(mpz(1<<32), "c")
if sys.version_info >= (3, 14):
with pytest.raises(ValueError,
match="Cannot specify both ',' and '_'."):
match=r"Cannot specify both ',' and '_'."):
format(mx, ".10,_f")
with pytest.raises(ValueError,
match="Cannot specify both ',' and '_'."):
match=r"Cannot specify both ',' and '_'."):
format(mx, ".10_,f")
with pytest.raises(ValueError, match="Cannot specify '_' with 'n'."):
with pytest.raises(ValueError, match=r"Cannot specify '_' with 'n'."):
format(mx, ".10_n")
with pytest.raises(ValueError, match="Cannot specify ',' with 'n'."):
with pytest.raises(ValueError, match=r"Cannot specify ',' with 'n'."):
format(mx, ".10,n")
assert format(mx, ".2f") == "123.00"
assert format(mx, "") == "123"
Expand Down Expand Up @@ -366,9 +370,9 @@ def test_richcompare_mixed(x, y):
def test_richcompare_errors():
mx = mpz(123)
with pytest.raises(TypeError):
mx > 1j
assert mx > 1j
with pytest.raises(TypeError):
mx > object()
assert mx > object()


def test_hash_caching():
Expand Down Expand Up @@ -604,8 +608,10 @@ def test_truediv_mixed(x, y):

def test_truediv_errors():
mx = mpz(123)
pytest.raises(TypeError, lambda: mx / object())
pytest.raises(TypeError, lambda: object() / mx)
with pytest.raises(TypeError):
mx / object()
with pytest.raises(TypeError):
object() / mx


@given(bigints(), integers(max_value=100000))
Expand Down Expand Up @@ -980,7 +986,7 @@ def test_round_interface():
with pytest.raises(OverflowError):
x.__round__((-1<<64) + 1)
with pytest.raises(OverflowError):
x.__round__((-1<<62))
x.__round__(-1<<62)
with pytest.raises(OverflowError):
x.__round__(-1<<127)

Expand Down
10 changes: 5 additions & 5 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def python_truediv(a, b):

# find integer d satisfying 2**(d - 1) <= a/b < 2**d
d = a.bit_length() - b.bit_length()
if d >= 0 and a >= 2**d * b or d < 0 and a * 2**-d >= b:
if (d >= 0 and a >= 2**d * b) or (d < 0 and a * 2**-d >= b):
d += 1

# compute 2**-exp * a / b for suitable exp
Expand All @@ -79,7 +79,7 @@ def python_truediv(a, b):

# round-half-to-even: fractional part is r/b, which is > 0.5 iff
# 2*r > b, and == 0.5 iff 2*r == b.
if 2*r > b or 2*r == b and q % 2 == 1:
if 2*r > b or (2*r == b and q % 2 == 1):
q += 1

result = math.ldexp(q, exp)
Expand Down Expand Up @@ -118,15 +118,15 @@ def fmt_str(draw, types="bdoxXn"):
align = draw(sampled_from(list("<^>=")))
res += fill_char + align
else:
align = draw(sampled_from([""] + list("<^>=")))
align = draw(sampled_from(["", *list("<^>=")]))
if align:
skip_0_padding = True
res += align
else:
skip_0_padding = False

# sign character
res += draw(sampled_from([""] + list("-+ ")))
res += draw(sampled_from(["", *list("-+ ")]))

# alternate mode
res += draw(sampled_from(["", "#"]))
Expand All @@ -142,7 +142,7 @@ def fmt_str(draw, types="bdoxXn"):
res += draw(sampled_from([""]*7 + list(map(str, range(1, 40)))))

# grouping character (thousand_separators)
gchar = draw(sampled_from([""] + list(",_")))
gchar = draw(sampled_from(["", *list(",_")]))
if (gchar and not skip_thousand_separators
and not (gchar == "," and type in ["b", "o", "x", "X"])
and type != "n"):
Expand Down