Skip to content

Commit 6b0dc30

Browse files
authored
Merge pull request #3158 from Zac-HD/pprint-weird-nans
Distinguish negative and signaling NaNs in our pretty-printer
2 parents 679635c + e060521 commit 6b0dc30

6 files changed

Lines changed: 68 additions & 32 deletions

File tree

‎hypothesis-python/RELEASE.rst‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RELEASE_TYPE: minor
2+
3+
Did you know that of the 2\ :superscript:`64` possible floating-point numbers,
4+
2\ :superscript:`53` of them are ``nan`` - and Python prints them all the same way?
5+
6+
While nans *usually* have all zeros in the sign bit and mantissa, this
7+
`isn't always true <https://wingolog.org/archives/2011/05/18/value-representation-in-javascript-implementations>`__,
8+
and :wikipedia:`'signaling' nans might trap or error <https://en.wikipedia.org/wiki/NaN#Signaling_NaN>`.
9+
To help distinguish such errors in e.g. CI logs, Hypothesis now prints ``-nan`` for
10+
negative nans, and adds a comment like ``# Saw 3 signaling NaNs`` if applicable.

‎hypothesis-python/docs/changes.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ like ``s.map(lambda x: x)`` and ``lists().filter(len)`` more efficient
3434
6.24.6 - 2021-11-18
3535
-------------------
3636

37-
This patch makes :func:`hypothesis.strategies.floats` generate
37+
This patch makes :func:`~hypothesis.strategies.floats` generate
3838
:wikipedia:`"subnormal" floating point numbers <Subnormal_number>`
3939
more often, as these rare values can have strange interactions with
4040
`unsafe compiler optimisations like -ffast-math
@@ -47,7 +47,7 @@ more often, as these rare values can have strange interactions with
4747
6.24.5 - 2021-11-16
4848
-------------------
4949

50-
This patch fixes a rare internal error in the :func:`hypothesis.strategies.datetimes`
50+
This patch fixes a rare internal error in the :func:`~hypothesis.strategies.datetimes`
5151
strategy, where the implementation of ``allow_imaginary=False`` crashed when checking
5252
a time during the skipped hour of a DST transition *if* the DST offset is negative -
5353
only true of ``Europe/Dublin``, who we presume have their reasons - and the ``tzinfo``

‎hypothesis-python/src/hypothesis/strategies/_internal/numbers.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def integers(
147147
return IntegersStrategy(min_value, max_value)
148148

149149

150+
SIGNALING_NAN = int_to_float(0x7FF8_0000_0000_0001) # nonzero mantissa
151+
assert math.isnan(SIGNALING_NAN) and math.copysign(1, SIGNALING_NAN) == 1
152+
150153
NASTY_FLOATS = sorted(
151154
[
152155
0.0,
@@ -170,7 +173,8 @@ def integers(
170173
]
171174
+ [2.0 ** -n for n in (24, 14, 149, 126)] # minimum (sub)normals for float16,32
172175
+ [float_info.min / n for n in (2, 10, 1000, 100_000)] # subnormal in float64
173-
+ [math.inf, math.nan] * 5,
176+
+ [math.inf, math.nan] * 5
177+
+ [SIGNALING_NAN],
174178
key=flt.float_to_lex,
175179
)
176180
NASTY_FLOATS = list(map(float, NASTY_FLOATS))

‎hypothesis-python/src/hypothesis/vendor/pretty.py‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
This module is based on ruby's `prettyprint.rb` library by `Tanaka Akira`.
2121
Example Usage
2222
-------------
23-
To directly print the representation of an object use `pprint`::
24-
from pretty import pprint
25-
pprint(complex_object)
2623
To get a string of the output use `pretty`::
2724
from pretty import pretty
2825
string = pretty(complex_object)
@@ -73,15 +70,15 @@ def _repr_pretty_(self, p, cycle):
7370
import datetime
7471
import platform
7572
import re
76-
import sys
73+
import struct
7774
import types
7875
from collections import deque
7976
from contextlib import contextmanager
8077
from io import StringIO
78+
from math import copysign, isnan
8179

8280
__all__ = [
8381
"pretty",
84-
"pprint",
8582
"PrettyPrinter",
8683
"RepresentationPrinter",
8784
"for_type_by_name",
@@ -120,19 +117,6 @@ def pretty(
120117
return stream.getvalue()
121118

122119

123-
def pprint(
124-
obj, verbose=False, max_width=79, newline="\n", max_seq_length=MAX_SEQ_LENGTH
125-
):
126-
"""Like `pretty` but print to stdout."""
127-
printer = RepresentationPrinter(
128-
sys.stdout, verbose, max_width, newline, max_seq_length=max_seq_length
129-
)
130-
printer.pretty(obj)
131-
printer.flush()
132-
sys.stdout.write(newline)
133-
sys.stdout.flush()
134-
135-
136120
class _PrettyPrinterBase:
137121
@contextmanager
138122
def indent(self, indent):
@@ -179,6 +163,8 @@ def __init__(
179163
self.group_queue = GroupQueue(root_group)
180164
self.indentation = 0
181165

166+
self.snans = 0
167+
182168
def _break_outer_groups(self):
183169
while self.max_width < self.output_width + self.buffer_width:
184170
group = self.group_queue.deq()
@@ -284,6 +270,12 @@ def end_group(self, dedent=0, close=""):
284270

285271
def flush(self):
286272
"""Flush data that is left in the buffer."""
273+
if self.snans:
274+
# Reset self.snans *before* calling breakable(), which might flush()
275+
snans = self.snans
276+
self.snans = 0
277+
self.breakable(" ")
278+
self.text(f"# Saw {snans} signaling NaN" + "s" * (snans > 1))
287279
for data in self.buffer:
288280
self.output_width += data.output(self.output, self.output_width)
289281
self.buffer.clear()
@@ -753,10 +745,20 @@ def _exception_pprint(obj, p, cycle):
753745
p.end_group(step, ")")
754746

755747

748+
def _repr_float_counting_nans(obj, p, cycle):
749+
if isnan(obj) and hasattr(p, "snans"):
750+
if struct.pack("!d", abs(obj)) != struct.pack("!d", float("nan")):
751+
p.snans += 1
752+
if copysign(1.0, obj) == -1.0:
753+
p.text("-nan")
754+
return
755+
p.text(repr(obj))
756+
757+
756758
#: printers for builtin types
757759
_type_pprinters = {
758760
int: _repr_pprint,
759-
float: _repr_pprint,
761+
float: _repr_float_counting_nans,
760762
str: _repr_pprint,
761763
tuple: _seq_pprinter_factory("(", ")", tuple),
762764
list: _seq_pprinter_factory("[", "]", list),

‎hypothesis-python/tests/cover/test_pretty.py‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@
5959
import pytest
6060

6161
from hypothesis.internal.compat import PYPY
62+
from hypothesis.strategies._internal.numbers import SIGNALING_NAN
6263
from hypothesis.vendor import pretty
6364

64-
from tests.common.utils import capture_out
65-
6665

6766
class MyList:
6867
def __init__(self, content):
@@ -546,13 +545,6 @@ def test_cyclic_set():
546545
assert pretty.pretty(x) == "{{...}}"
547546

548547

549-
def test_pprint():
550-
t = {"hi": 1}
551-
with capture_out() as o:
552-
pretty.pprint(t)
553-
assert o.getvalue().strip() == pretty.pretty(t)
554-
555-
556548
class BigList(list):
557549
def _repr_pretty_(self, printer, cycle):
558550
if cycle:
@@ -631,3 +623,17 @@ def test_empty_printer():
631623

632624
def test_breakable_at_group_boundary():
633625
assert "\n" in pretty.pretty([[], "000000"], max_width=5)
626+
627+
628+
@pytest.mark.parametrize(
629+
"obj, rep",
630+
[
631+
(float("nan"), "nan"),
632+
(-float("nan"), "-nan"),
633+
(SIGNALING_NAN, "nan # Saw 1 signaling NaN"),
634+
(-SIGNALING_NAN, "-nan # Saw 1 signaling NaN"),
635+
((SIGNALING_NAN, SIGNALING_NAN), "(nan, nan) # Saw 2 signaling NaNs"),
636+
],
637+
)
638+
def test_nan_reprs(obj, rep):
639+
assert pretty.pretty(obj) == rep

‎hypothesis-python/tests/nocover/test_floating.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
import pytest
2222

2323
from hypothesis import HealthCheck, assume, given, settings
24-
from hypothesis.internal.floats import next_down
24+
from hypothesis.internal.floats import float_to_int, next_down
2525
from hypothesis.strategies import data, floats, lists
2626

27+
from tests.common.debug import find_any
2728
from tests.common.utils import fails
2829

2930
TRY_HARDER = settings(
@@ -161,3 +162,16 @@ def test_floats_are_in_range(x, y, data):
161162

162163
t = data.draw(floats(x, y))
163164
assert x <= t <= y
165+
166+
167+
@pytest.mark.parametrize("neg", [False, True])
168+
@pytest.mark.parametrize("snan", [False, True])
169+
def test_can_find_negative_and_signaling_nans(neg, snan):
170+
find_any(
171+
floats().filter(math.isnan),
172+
lambda x: (
173+
snan is (float_to_int(abs(x)) != float_to_int(float("nan")))
174+
and neg is (math.copysign(1, x) == -1)
175+
),
176+
settings=TRY_HARDER,
177+
)

0 commit comments

Comments
 (0)