Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 10 additions & 1 deletion Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -789,14 +789,23 @@ wave
which were deprecated since Python 3.13.
(Contributed by Bénédikt Tran in :gh:`133873`.)


zipimport
---------

* Remove deprecated :meth:`!zipimport.zipimporter.load_module`.
Use :meth:`zipimport.zipimporter.exec_module` instead.
(Contributed by Jiahao Li in :gh:`133656`.)

Others
------

* Removed support for handling returned
non-:class:`float`/:class:`complex` types from
:meth:`~object.__float__` and :meth:`~object.__complex__`,
respectively. This had previously raised a
:exc:`DeprecationWarning` since Python 3.7.
(Contributed by Sergey B Kirpichev in :gh:`109311`.)


Porting to Python 3.15
======================
Expand Down
28 changes: 6 additions & 22 deletions Lib/test/test_capi/test_complex.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from math import isnan
import errno
import unittest
import warnings

from test.test_capi.test_getargs import (BadComplex, BadComplex2, Complex,
FloatSubclass, Float, BadFloat,
Expand Down Expand Up @@ -82,18 +81,13 @@ def test_realasdouble(self):
# Test types with __complex__ dunder method
self.assertEqual(realasdouble(Complex()), 4.25)
self.assertRaises(TypeError, realasdouble, BadComplex())
with self.assertWarns(DeprecationWarning):
self.assertEqual(realasdouble(BadComplex2()), 4.25)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, realasdouble, BadComplex2())
self.assertRaises(TypeError, realasdouble, BadComplex2())
self.assertRaises(RuntimeError, realasdouble, BadComplex3())

# 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, BadFloat2())

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

Expand All @@ -115,18 +109,13 @@ def test_imagasdouble(self):
# Test types with __complex__ dunder method
self.assertEqual(imagasdouble(Complex()), 0.5)
self.assertRaises(TypeError, imagasdouble, BadComplex())
with self.assertWarns(DeprecationWarning):
self.assertEqual(imagasdouble(BadComplex2()), 0.5)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, imagasdouble, BadComplex2())
self.assertRaises(TypeError, imagasdouble, BadComplex2())
self.assertRaises(RuntimeError, imagasdouble, BadComplex3())

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

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

Expand All @@ -150,18 +139,13 @@ def test_asccomplex(self):
# 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(TypeError, 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, BadFloat2())

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

Expand Down
7 changes: 1 addition & 6 deletions Lib/test/test_capi/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import random
import sys
import unittest
import warnings

from test.test_capi.test_getargs import (Float, FloatSubclass, FloatSubclass2,
BadIndex2, BadFloat2, Index, BadIndex,
Expand Down Expand Up @@ -101,11 +100,7 @@ def __float__(self):
self.assertRaises(RuntimeError, asdouble, BadFloat3())
with self.assertWarns(DeprecationWarning):
self.assertEqual(asdouble(BadIndex2()), 1.)
with self.assertWarns(DeprecationWarning):
self.assertEqual(asdouble(BadFloat2()), 4.25)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, asdouble, BadFloat2())
self.assertRaises(TypeError, asdouble, BadFloat2())
self.assertRaises(TypeError, asdouble, object())
self.assertRaises(TypeError, asdouble, NULL)

Expand Down
9 changes: 3 additions & 6 deletions Lib/test/test_capi/test_getargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,7 @@ def test_f(self):
self.assertEqual(getargs_f(FloatSubclass(7.5)), 7.5)
self.assertEqual(getargs_f(FloatSubclass2(7.5)), 7.5)
self.assertRaises(TypeError, getargs_f, BadFloat())
with self.assertWarns(DeprecationWarning):
self.assertEqual(getargs_f(BadFloat2()), 4.25)
self.assertRaises(TypeError, getargs_f, BadFloat2())
self.assertEqual(getargs_f(BadFloat3(7.5)), 7.5)
self.assertEqual(getargs_f(Index()), 99.0)
self.assertRaises(TypeError, getargs_f, Int())
Expand Down Expand Up @@ -525,8 +524,7 @@ def test_d(self):
self.assertEqual(getargs_d(FloatSubclass(7.5)), 7.5)
self.assertEqual(getargs_d(FloatSubclass2(7.5)), 7.5)
self.assertRaises(TypeError, getargs_d, BadFloat())
with self.assertWarns(DeprecationWarning):
self.assertEqual(getargs_d(BadFloat2()), 4.25)
self.assertRaises(TypeError, getargs_d, BadFloat2())
self.assertEqual(getargs_d(BadFloat3(7.5)), 7.5)
self.assertEqual(getargs_d(Index()), 99.0)
self.assertRaises(TypeError, getargs_d, Int())
Expand All @@ -549,8 +547,7 @@ def test_D(self):
self.assertEqual(getargs_D(ComplexSubclass(7.5+0.25j)), 7.5+0.25j)
self.assertEqual(getargs_D(ComplexSubclass2(7.5+0.25j)), 7.5+0.25j)
self.assertRaises(TypeError, getargs_D, BadComplex())
with self.assertWarns(DeprecationWarning):
self.assertEqual(getargs_D(BadComplex2()), 4.25+0.5j)
self.assertRaises(TypeError, getargs_D, BadComplex2())
self.assertEqual(getargs_D(BadComplex3(7.5+0.25j)), 7.5+0.25j)
self.assertEqual(getargs_D(Index()), 99.0+0j)
self.assertRaises(TypeError, getargs_D, Int())
Expand Down
6 changes: 1 addition & 5 deletions Lib/test/test_capi/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,7 @@ def test_float(self):
self.assertEqual(float_(IndexLike.with_val(-1)), -1.0)

self.assertRaises(TypeError, float_, FloatLike.with_val(687))
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, float_, FloatLike.with_val(subclassof(float)(4.25)))
with self.assertWarns(DeprecationWarning):
self.assertEqual(float_(FloatLike.with_val(subclassof(float)(4.25))), 4.25)
self.assertRaises(TypeError, float_, FloatLike.with_val(subclassof(float)(4.25)))
self.assertRaises(RuntimeError, float_, FloatLike.with_exc(RuntimeError))

self.assertRaises(TypeError, float_, IndexLike.with_val(1.25))
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,7 @@ def __complex__(self):
return None

check(complex(complex0(1j)), 0.0, 42.0)
with self.assertWarns(DeprecationWarning):
check(complex(complex1(1j)), 0.0, 2.0)
self.assertRaises(TypeError, complex, complex1(1j))
self.assertRaises(TypeError, complex, complex2(1j))

def test___complex__(self):
Expand Down
13 changes: 3 additions & 10 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,16 @@ def __float__(self):

self.assertEqual(float(FloatLike(42.)), 42.)
self.assertEqual(float(Foo2()), 42.)
with self.assertWarns(DeprecationWarning):
self.assertEqual(float(Foo3(21)), 42.)
self.assertRaises(TypeError, float, Foo3(21))
self.assertRaises(TypeError, float, Foo4(42))
self.assertEqual(float(FooStr('8')), 9.)

self.assertRaises(TypeError, time.sleep, FloatLike(""))

# Issue #24731
f = FloatLike(OtherFloatSubclass(42.))
with self.assertWarns(DeprecationWarning):
self.assertEqual(float(f), 42.)
with self.assertWarns(DeprecationWarning):
self.assertIs(type(float(f)), float)
with self.assertWarns(DeprecationWarning):
self.assertEqual(FloatSubclass(f), 42.)
with self.assertWarns(DeprecationWarning):
self.assertIs(type(FloatSubclass(f)), FloatSubclass)
self.assertRaises(TypeError, float, f)
self.assertRaises(TypeError, FloatSubclass, f)

self.assertEqual(float(MyIndex(42)), 42.0)
self.assertRaises(OverflowError, float, MyIndex(2**2000))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Remove support for returning non-:class:`complex` or non-:class:`float`
objects in :meth:`~object.__complex__` and :meth:`~object.__float__`.
20 changes: 3 additions & 17 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1609,24 +1609,10 @@ PyNumber_Float(PyObject *o)
return res;
}

if (!PyFloat_Check(res)) {
PyErr_Format(PyExc_TypeError,
"%T.__float__() must return a float, not %T", o, res);
Py_DECREF(res);
return NULL;
}
/* Issue #26983: warn if 'res' not of exact type float. */
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"%T.__float__() must return a float, not %T. "
"The ability to return an instance of a strict subclass of float "
"is deprecated, and may be removed in a future version of Python.",
o, res)) {
Py_DECREF(res);
return NULL;
}
double val = PyFloat_AS_DOUBLE(res);
PyErr_Format(PyExc_TypeError,
"%T.__float__() must return a float, not %T", o, res);
Py_DECREF(res);
return PyFloat_FromDouble(val);
return NULL;
}

if (m && m->nb_index) {
Expand Down
22 changes: 5 additions & 17 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,23 +513,11 @@ try_complex_special_method(PyObject *op)
if (!res || PyComplex_CheckExact(res)) {
return res;
}
if (!PyComplex_Check(res)) {
PyErr_Format(PyExc_TypeError,
"%T.__complex__() must return a complex, not %T",
op, res);
Py_DECREF(res);
return NULL;
}
/* Issue #29894: warn if 'res' not of exact type complex. */
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"%T.__complex__() must return a complex, not %T. "
"The ability to return an instance of a strict subclass of complex "
"is deprecated, and may be removed in a future version of Python.",
op, res)) {
Py_DECREF(res);
return NULL;
}
return res;
PyErr_Format(PyExc_TypeError,
"%T.__complex__() must return a complex, not %T",
op, res);
Py_DECREF(res);
return NULL;
}
return NULL;
}
Expand Down
20 changes: 5 additions & 15 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,21 +286,11 @@ PyFloat_AsDouble(PyObject *op)
return -1;
}
if (!PyFloat_CheckExact(res)) {
if (!PyFloat_Check(res)) {
PyErr_Format(PyExc_TypeError,
"%T.__float__() must return a float, not %T",
op, res);
Py_DECREF(res);
return -1;
}
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"%T.__float__() must return a float, not %T. "
"The ability to return an instance of a strict subclass of float "
"is deprecated, and may be removed in a future version of Python.",
op, res)) {
Py_DECREF(res);
return -1;
}
PyErr_Format(PyExc_TypeError,
"%T.__float__() must return a float, not %T",
op, res);
Py_DECREF(res);
return -1;
}

val = PyFloat_AS_DOUBLE(res);
Expand Down
Loading