-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-111495: Add tests for PyComplex C API #111591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b88f657
gh-111495: Add tests for PyComplex C API
skirpichev 9f04bcb
Simplify complex_fromdoubles()
skirpichev d19a9d4
Use assertEqual()
skirpichev c8adbd2
Move tests to Lib/test/test_capi/test_complex.py
skirpichev 6ead74b
Add missing coverage tests
skirpichev 67fb214
Cleanup, add comments
skirpichev 7694c60
Apply suggestions from code review
skirpichev 70796bd
+1
skirpichev 5c1cd4f
Correct typos and wrongly added test
skirpichev 20e37e5
Drop comments
skirpichev b743f4b
Use PyComplex_AsCComplex() in complex_fromccomplex()
skirpichev 89a18ff
Merge branch 'main' into capi-complex-tests
skirpichev 34347fa
Use PyArg_Parse('D') instead
skirpichev 09f5246
s/3.14/4.25/g
skirpichev 3682e51
Reorder statements in test_as* and add an integer test for imagasdoub…
skirpichev a4981d6
Apply suggestions from code review
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import unittest | ||
import warnings | ||
|
||
from test.test_capi.test_getargs import (BadComplex, BadComplex2, Complex, | ||
FloatSubclass, Float, BadFloat, | ||
BadFloat2, ComplexSubclass) | ||
from test.support import import_helper | ||
|
||
|
||
_testcapi = import_helper.import_module('_testcapi') | ||
|
||
NULL = None | ||
|
||
class BadComplex3: | ||
def __complex__(self): | ||
raise RuntimeError | ||
|
||
|
||
class CAPIComplexTest(unittest.TestCase): | ||
def test_check(self): | ||
# Test PyComplex_Check() | ||
check = _testcapi.complex_check | ||
|
||
self.assertTrue(check(1+2j)) | ||
self.assertTrue(check(ComplexSubclass(1+2j))) | ||
self.assertFalse(check(Complex())) | ||
self.assertFalse(check(3)) | ||
self.assertFalse(check(3.0)) | ||
self.assertFalse(check(object())) | ||
|
||
# CRASHES check(NULL) | ||
|
||
def test_checkexact(self): | ||
# PyComplex_CheckExact() | ||
checkexact = _testcapi.complex_checkexact | ||
|
||
self.assertTrue(checkexact(1+2j)) | ||
self.assertFalse(checkexact(ComplexSubclass(1+2j))) | ||
self.assertFalse(checkexact(Complex())) | ||
self.assertFalse(checkexact(3)) | ||
self.assertFalse(checkexact(3.0)) | ||
self.assertFalse(checkexact(object())) | ||
|
||
# CRASHES checkexact(NULL) | ||
|
||
def test_fromccomplex(self): | ||
# Test PyComplex_FromCComplex() | ||
fromccomplex = _testcapi.complex_fromccomplex | ||
|
||
self.assertEqual(fromccomplex(1+2j), 1.0+2.0j) | ||
|
||
def test_fromdoubles(self): | ||
# Test PyComplex_FromDoubles() | ||
fromdoubles = _testcapi.complex_fromdoubles | ||
|
||
self.assertEqual(fromdoubles(1.0, 2.0), 1.0+2.0j) | ||
|
||
def test_realasdouble(self): | ||
# Test PyComplex_RealAsDouble() | ||
realasdouble = _testcapi.complex_realasdouble | ||
|
||
self.assertEqual(realasdouble(1+2j), 1.0) | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(realasdouble(-1+0j), -1.0) | ||
self.assertEqual(realasdouble(4.25), 4.25) | ||
self.assertEqual(realasdouble(-1.0), -1.0) | ||
self.assertEqual(realasdouble(42), 42.) | ||
self.assertEqual(realasdouble(-1), -1.0) | ||
|
||
# Test subclasses of complex/float | ||
self.assertEqual(realasdouble(ComplexSubclass(1+2j)), 1.0) | ||
self.assertEqual(realasdouble(FloatSubclass(4.25)), 4.25) | ||
|
||
# Test types with __complex__ dunder method | ||
# Function doesn't support classes with __complex__ dunder, see #109598 | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertRaises(TypeError, realasdouble, Complex()) | ||
|
||
# Test types with __float__ dunder method | ||
self.assertEqual(realasdouble(Float()), 4.25) | ||
self.assertRaises(TypeError, realasdouble, BadFloat()) | ||
with self.assertWarns(DeprecationWarning): | ||
self.assertEqual(realasdouble(BadFloat2()), 4.25) | ||
|
||
self.assertRaises(TypeError, realasdouble, object()) | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# CRASHES realasdouble(NULL) | ||
|
||
def test_imagasdouble(self): | ||
# Test PyComplex_ImagAsDouble() | ||
imagasdouble = _testcapi.complex_imagasdouble | ||
|
||
self.assertEqual(imagasdouble(1+2j), 2.0) | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(imagasdouble(1-1j), -1.0) | ||
self.assertEqual(imagasdouble(4.25), 0.0) | ||
self.assertEqual(imagasdouble(42), 0.0) | ||
|
||
# Test subclasses of complex/float | ||
self.assertEqual(imagasdouble(ComplexSubclass(1+2j)), 2.0) | ||
self.assertEqual(imagasdouble(FloatSubclass(4.25)), 0.0) | ||
|
||
# Test types with __complex__ dunder method | ||
# Function doesn't support classes with __complex__ dunder, see #109598 | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(imagasdouble(Complex()), 0.0) | ||
|
||
# Function returns 0.0 anyway, see #109598 | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(imagasdouble(object()), 0.0) | ||
|
||
# CRASHES imagasdouble(NULL) | ||
|
||
def test_asccomplex(self): | ||
# Test PyComplex_AsCComplex() | ||
asccomplex = _testcapi.complex_asccomplex | ||
|
||
self.assertEqual(asccomplex(1+2j), 1.0+2.0j) | ||
self.assertEqual(asccomplex(-1+2j), -1.0+2.0j) | ||
self.assertEqual(asccomplex(4.25), 4.25+0.0j) | ||
self.assertEqual(asccomplex(-1.0), -1.0+0.0j) | ||
self.assertEqual(asccomplex(42), 42+0j) | ||
self.assertEqual(asccomplex(-1), -1.0+0.0j) | ||
|
||
# Test subclasses of complex/float | ||
self.assertEqual(asccomplex(ComplexSubclass(1+2j)), 1.0+2.0j) | ||
self.assertEqual(asccomplex(FloatSubclass(4.25)), 4.25+0.0j) | ||
|
||
# Test types with __complex__ dunder method | ||
self.assertEqual(asccomplex(Complex()), 4.25+0.5j) | ||
self.assertRaises(TypeError, asccomplex, BadComplex()) | ||
with self.assertWarns(DeprecationWarning): | ||
self.assertEqual(asccomplex(BadComplex2()), 4.25+0.5j) | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("error", DeprecationWarning) | ||
self.assertRaises(DeprecationWarning, asccomplex, BadComplex2()) | ||
self.assertRaises(RuntimeError, asccomplex, BadComplex3()) | ||
|
||
# Test types with __float__ dunder method | ||
self.assertEqual(asccomplex(Float()), 4.25+0.0j) | ||
self.assertRaises(TypeError, asccomplex, BadFloat()) | ||
with self.assertWarns(DeprecationWarning): | ||
self.assertEqual(asccomplex(BadFloat2()), 4.25+0.0j) | ||
|
||
self.assertRaises(TypeError, asccomplex, object()) | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# CRASHES asccomplex(NULL) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.