Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
71 changes: 71 additions & 0 deletions Lib/test/support/number_helper.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
# Common test classes for numeric types.

class WithIndex:
def __init__(self, value):
self.value = value

def __index__(self):
return self.value


class IntSubclass(int):
pass

class WithInt:
def __init__(self, value):
self.value = value

def __int__(self):
return self.value

class WithIntAndIndex:
def __init__(self, value):
self.value = value

def __index__(self):
return self.value

def __int__(self):
return self.value + 12


class FloatSubclass(float):
pass

class OtherFloatSubclass(float):
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if it's worth it to have OtherFloatSubclass and OtherComplexSubclass in this number_helper module.

pass

class WithFloat:
def __init__(self, value):
self.value = value

def __float__(self):
return self.value

class FloatLikeSubclass(float):
def __init__(self, value):
self.value = value

def __float__(self):
return self.value.__class__(self.value*2)


class ComplexSubclass(complex):
pass

class OtherComplexSubclass(complex):
pass

class WithComplex:
def __init__(self, value):
self.value = value

def __complex__(self):
return self.value

class ComplexLikeSubclass(complex):
def __init__(self, value):
self.value = value

def __complex__(self):
return self.value.__class__(self.value*2)
51 changes: 25 additions & 26 deletions Lib/test/test_capi/test_complex.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,10 +3,9 @@
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
from test.support.number_helper import (ComplexSubclass, FloatSubclass,
WithFloat, WithComplex)
from test.support.testcase import ComplexesAreIdenticalMixin


Expand All@@ -31,7 +30,7 @@ def test_check(self):

self.assertTrue(check(1+2j))
self.assertTrue(check(ComplexSubclass(1+2j)))
self.assertFalse(check(Complex()))
self.assertFalse(check(WithComplex(4.25+0.5j)))
self.assertFalse(check(3))
self.assertFalse(check(3.0))
self.assertFalse(check(object()))
Expand All@@ -44,7 +43,7 @@ def test_checkexact(self):

self.assertTrue(checkexact(1+2j))
self.assertFalse(checkexact(ComplexSubclass(1+2j)))
self.assertFalse(checkexact(Complex()))
self.assertFalse(checkexact(WithComplex(4.25+0.5j)))
self.assertFalse(checkexact(3))
self.assertFalse(checkexact(3.0))
self.assertFalse(checkexact(object()))
Expand DownExpand Up@@ -79,20 +78,20 @@ def test_realasdouble(self):
self.assertEqual(realasdouble(FloatSubclass(4.25)), 4.25)

# Test types with __complex__ dunder method
self.assertEqual(realasdouble(Complex()), 4.25)
self.assertRaises(TypeError, realasdouble, BadComplex())
self.assertEqual(realasdouble(WithComplex(4.25+0.5j)), 4.25)
self.assertRaises(TypeError, realasdouble, WithComplex(1.25))
with self.assertWarns(DeprecationWarning):
self.assertEqual(realasdouble(BadComplex2()), 4.25)
self.assertEqual(realasdouble(WithComplex(ComplexSubclass(4.25+0.5j))), 4.25)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, realasdouble, BadComplex2())
self.assertRaises(DeprecationWarning, realasdouble, WithComplex(ComplexSubclass(4.25+0.5j)))
self.assertRaises(RuntimeError, realasdouble, BadComplex3())

# Test types with __float__ dunder method
self.assertEqual(realasdouble(Float()), 4.25)
self.assertRaises(TypeError, realasdouble, BadFloat())
self.assertEqual(realasdouble(WithFloat(4.25)), 4.25)
self.assertRaises(TypeError, realasdouble, WithFloat(687))
with self.assertWarns(DeprecationWarning):
self.assertEqual(realasdouble(BadFloat2()), 4.25)
self.assertEqual(realasdouble(WithFloat(FloatSubclass(4.25))), 4.25)

self.assertRaises(TypeError, realasdouble, object())

Expand All@@ -112,20 +111,20 @@ def test_imagasdouble(self):
self.assertEqual(imagasdouble(FloatSubclass(4.25)), 0.0)

# Test types with __complex__ dunder method
self.assertEqual(imagasdouble(Complex()), 0.5)
self.assertRaises(TypeError, imagasdouble, BadComplex())
self.assertEqual(imagasdouble(WithComplex(4.25+0.5j)), 0.5)
self.assertRaises(TypeError, imagasdouble, WithComplex(1.25))
with self.assertWarns(DeprecationWarning):
self.assertEqual(imagasdouble(BadComplex2()), 0.5)
self.assertEqual(imagasdouble(WithComplex(ComplexSubclass(4.25+0.5j))), 0.5)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, imagasdouble, BadComplex2())
self.assertRaises(DeprecationWarning, imagasdouble, WithComplex(ComplexSubclass(4.25+0.5j)))
self.assertRaises(RuntimeError, imagasdouble, BadComplex3())

# Test types with __float__ dunder method
self.assertEqual(imagasdouble(Float()), 0.0)
self.assertRaises(TypeError, imagasdouble, BadFloat())
self.assertEqual(imagasdouble(WithFloat(4.25)), 0.0)
self.assertRaises(TypeError, imagasdouble, WithFloat(687))
with self.assertWarns(DeprecationWarning):
self.assertEqual(imagasdouble(BadFloat2()), 0.0)
self.assertEqual(imagasdouble(WithFloat(FloatSubclass(4.25))), 0.0)

self.assertRaises(TypeError, imagasdouble, object())

Expand All@@ -147,20 +146,20 @@ def test_asccomplex(self):
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())
self.assertEqual(asccomplex(WithComplex(4.25+0.5j)), 4.25+0.5j)
self.assertRaises(TypeError, asccomplex, WithComplex(1.25))
with self.assertWarns(DeprecationWarning):
self.assertEqual(asccomplex(BadComplex2()), 4.25+0.5j)
self.assertEqual(asccomplex(WithComplex(ComplexSubclass(4.25+0.5j))), 4.25+0.5j)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, asccomplex, BadComplex2())
self.assertRaises(DeprecationWarning, asccomplex, WithComplex(ComplexSubclass(4.25+0.5j)))
self.assertRaises(RuntimeError, asccomplex, BadComplex3())

# Test types with __float__ dunder method
self.assertEqual(asccomplex(Float()), 4.25+0.0j)
self.assertRaises(TypeError, asccomplex, BadFloat())
self.assertEqual(asccomplex(WithFloat(4.25)), 4.25+0.0j)
self.assertRaises(TypeError, asccomplex, WithFloat(687))
with self.assertWarns(DeprecationWarning):
self.assertEqual(asccomplex(BadFloat2()), 4.25+0.0j)
self.assertEqual(asccomplex(WithFloat(FloatSubclass(4.25))), 4.25+0.0j)

self.assertRaises(TypeError, asccomplex, object())

Expand Down
24 changes: 13 additions & 11 deletions Lib/test/test_capi/test_float.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,10 +3,8 @@
import unittest
import warnings

from test.test_capi.test_getargs import (Float, FloatSubclass, FloatSubclass2,
BadIndex2, BadFloat2, Index, BadIndex,
BadFloat)
from test.support import import_helper
from test.support.number_helper import FloatSubclass, WithIndex, WithFloat

_testcapi = import_helper.import_module('_testcapi')
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
Expand All@@ -27,14 +25,18 @@
NAN = float("nan")


class FloatSubclass2(WithFloat, FloatSubclass):
pass


class CAPIFloatTest(unittest.TestCase):
def test_check(self):
# Test PyFloat_Check()
check = _testlimitedcapi.float_check

self.assertTrue(check(4.25))
self.assertTrue(check(FloatSubclass(4.25)))
self.assertFalse(check(Float()))
self.assertFalse(check(WithFloat(4.25)))
self.assertFalse(check(3))
self.assertFalse(check(object()))

Expand All@@ -46,7 +48,7 @@ def test_checkexact(self):

self.assertTrue(checkexact(4.25))
self.assertFalse(checkexact(FloatSubclass(4.25)))
self.assertFalse(checkexact(Float()))
self.assertFalse(checkexact(WithFloat(4.25)))
self.assertFalse(checkexact(3))
self.assertFalse(checkexact(object()))

Expand DownExpand Up@@ -93,18 +95,18 @@ def __float__(self):

self.assertEqual(asdouble(FloatSubclass(4.25)), 4.25)
self.assertEqual(asdouble(FloatSubclass2(4.25)), 4.25)
self.assertEqual(asdouble(Index()), 99.)
self.assertEqual(asdouble(WithIndex(99)), 99.)

self.assertRaises(TypeError, asdouble, BadIndex())
self.assertRaises(TypeError, asdouble, BadFloat())
self.assertRaises(TypeError, asdouble, WithIndex(1.0))
self.assertRaises(TypeError, asdouble, WithFloat(687))
self.assertRaises(RuntimeError, asdouble, BadFloat3())
with self.assertWarns(DeprecationWarning):
self.assertEqual(asdouble(BadIndex2()), 1.)
self.assertEqual(asdouble(WithIndex(True)), 1.)
with self.assertWarns(DeprecationWarning):
self.assertEqual(asdouble(BadFloat2()), 4.25)
self.assertEqual(asdouble(WithFloat(FloatSubclass(4.25))), 4.25)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, asdouble, BadFloat2())
self.assertRaises(DeprecationWarning, asdouble, WithFloat(FloatSubclass(4.25)))
self.assertRaises(TypeError, asdouble, object())
self.assertRaises(TypeError, asdouble, NULL)

Expand Down
Loading
Loading