gh-145117: Fix pprint.isreadable() returning True for inf/nan#145116
gh-145117: Fix pprint.isreadable() returning True for inf/nan#145116zetzschest wants to merge 2 commits intopython:mainfrom
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
The following commit authors need to sign the Contributor License Agreement: |
Lib/pprint.py
Outdated
| typ = type(object) | ||
| if typ is float: | ||
| import math | ||
| if math.isinf(object) or math.isnan(object): |
There was a problem hiding this comment.
not math.isfinite() would be equivalent
6fabca6 to
8d2e450
Compare
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
picnixz
left a comment
There was a problem hiding this comment.
Possible alternative though not necessarily better I think:
- Move the
typ is floatcheck in the_builtin_scalarsbranch. - Compute the
repr(object) - Compare with
-inf/inf/nanas strings. That could save an import here though it's not that costly.
However, please add tests.
inf, -inf, nan are not valid Python literals so eval() fails on them. isreadable() should return False for these values.
227a709 to
672b0f2
Compare
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Btw, please stop force-pushing (see our devguide: https://devguide.python.org/getting-started/pull-request-lifecycle/#don-t-force-push) |
|
Created a fresh PR (#145120) to avoid force-pushing. Apologies for the noise. |
Problem
pprint.isreadable()returnsTrueforfloat('inf'),float('-inf'), andfloat('nan'), but theirrepr()values are not valid Python literals —eval()raisesNameError. The documented contract ofisreadableis that the representation can be used to recreate the value viaeval().Reproducer
Fix
In
Lib/pprint.py, in_safe_repr, add a check for float specials and returnreadable=Falsefor them.