FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-111495: Add tests for PyFloat C API by skirpichev · Pull Request #111624 · python/cpython · GitHub

/ cpython Public

gh-111495: Add tests for PyFloat C API - #111624

Merged
serhiy-storchaka merged 13 commits into
python:mainfrom
skirpichev:capi-float-tests
Nov 5, 2023
Merged

gh-111495: Add tests for PyFloat C API#111624
serhiy-storchaka merged 13 commits into
python:mainfrom
skirpichev:capi-float-tests

Conversation

skirpichev commented Nov 2, 2023
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

bedevere-app Bot mentioned this pull request Nov 2, 2023
10 tasks

Copy link
Copy Markdown
Member Author

Some C API tests for floats are in Lib/test/test_float.py historically. An example:

cpython/Lib/test/test_float.py

Lines 1518 to 1569 in ed587be

@unittest.skipIf(_testcapi is None, 'needs _testcapi')
class PackTests(unittest.TestCase):
def test_pack(self):
self.assertEqual(_testcapi.float_pack(2, 1.5, BIG_ENDIAN),
b'>\x00')
self.assertEqual(_testcapi.float_pack(4, 1.5, BIG_ENDIAN),
b'?\xc0\x00\x00')
self.assertEqual(_testcapi.float_pack(8, 1.5, BIG_ENDIAN),
b'?\xf8\x00\x00\x00\x00\x00\x00')
self.assertEqual(_testcapi.float_pack(2, 1.5, LITTLE_ENDIAN),
b'\x00>')
self.assertEqual(_testcapi.float_pack(4, 1.5, LITTLE_ENDIAN),
b'\x00\x00\xc0?')
self.assertEqual(_testcapi.float_pack(8, 1.5, LITTLE_ENDIAN),
b'\x00\x00\x00\x00\x00\x00\xf8?')
def test_unpack(self):
self.assertEqual(_testcapi.float_unpack(b'>\x00', BIG_ENDIAN),
1.5)
self.assertEqual(_testcapi.float_unpack(b'?\xc0\x00\x00', BIG_ENDIAN),
1.5)
self.assertEqual(_testcapi.float_unpack(b'?\xf8\x00\x00\x00\x00\x00\x00', BIG_ENDIAN),
1.5)
self.assertEqual(_testcapi.float_unpack(b'\x00>', LITTLE_ENDIAN),
1.5)
self.assertEqual(_testcapi.float_unpack(b'\x00\x00\xc0?', LITTLE_ENDIAN),
1.5)
self.assertEqual(_testcapi.float_unpack(b'\x00\x00\x00\x00\x00\x00\xf8?', LITTLE_ENDIAN),
1.5)
def test_roundtrip(self):
large = 2.0 ** 100
values = [1.0, 1.5, large, 1.0/7, math.pi]
if HAVE_IEEE_754:
values.extend((INF, NAN))
for value in values:
for size in (2, 4, 8,):
if size == 2 and value == large:
# too large for 16-bit float
continue
rel_tol = EPSILON[size]
for endian in (BIG_ENDIAN, LITTLE_ENDIAN):
with self.subTest(value=value, size=size, endian=endian):
data = _testcapi.float_pack(size, value, endian)
value2 = _testcapi.float_unpack(data, endian)
if isnan(value):
self.assertTrue(isnan(value2), (value, value2))
elif size < 8:
self.assertTrue(math.isclose(value2, value, rel_tol=rel_tol),
(value, value2))
else:
self.assertEqual(value2, value)

In this PR this left intact, but maybe we should move them.

Comment thread Lib/test/test_capi/test_float.py Outdated
Comment thread Lib/test/test_capi/test_float.py Outdated

sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Just one suggestion :)
All other things LGTM.

Comment thread Lib/test/test_capi/test_float.py Outdated
Comment thread Lib/test/test_capi/test_float.py Outdated

Copy link
Copy Markdown
Member Author

Converted to a draft per review #111591

Comment thread Lib/test/test_capi/test_float.py Outdated
Comment thread Lib/test/test_capi/test_float.py Outdated
skirpichev marked this pull request as ready for review November 4, 2023 15:48
Comment thread Lib/test/test_capi/test_float.py Outdated
Comment thread Modules/_testcapi/float.c Outdated
Comment thread Lib/test/test_capi/test_float.py Outdated
Comment thread Lib/test/test_capi/test_float.py Outdated
Comment thread Lib/test/test_capi/test_float.py Outdated
Comment thread Lib/test/test_capi/test_float.py Outdated
Comment thread Modules/_testcapi/float.c Outdated

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM.

Thank you @skirpichev for your contribution and for your fast response.

serhiy-storchaka added tests Tests in the Lib/test dir skip news needs backport to 3.12 only security fixes topic-C-API labels Nov 5, 2023
serhiy-storchaka merged commit b452202 into python:main Nov 5, 2023

Copy link
Copy Markdown

Thanks @skirpichev for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 5, 2023
(cherry picked from commit b452202)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>

bedevere-app Bot commented Nov 5, 2023

Copy link
Copy Markdown

GH-111752 is a backport of this pull request to the 3.12 branch.

bedevere-app Bot removed the needs backport to 3.12 only security fixes label Nov 5, 2023
skirpichev deleted the capi-float-tests branch November 5, 2023 07:21
serhiy-storchaka pushed a commit that referenced this pull request Nov 5, 2023
(cherry picked from commit b452202)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>

Copy link
Copy Markdown
Member Author

@serhiy-storchaka, thank you for review.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news tests Tests in the Lib/test dir topic-C-API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL