Skip to content

Commit 9288420

Browse files
vstinnerasvetlov
authored andcommitted
bpo-46852: Remove the float.__set_format__() method (GH-31585)
Remove the undocumented private float.__set_format__() method, previously known as float.__set_format__() in Python 3.7. Its docstring said: "You probably don't want to use this function. It exists mainly to be used in Python's test suite."
1 parent 2af3e4b commit 9288420

File tree

7 files changed

+15
-214
lines changed

7 files changed

+15
-214
lines changed

Doc/library/unittest.mock.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ Magic methods that are supported but not setup by default in ``MagicMock`` are:
21652165
* ``__reversed__`` and ``__missing__``
21662166
* ``__reduce__``, ``__reduce_ex__``, ``__getinitargs__``, ``__getnewargs__``,
21672167
``__getstate__`` and ``__setstate__``
2168-
* ``__getformat__`` and ``__setformat__``
2168+
* ``__getformat__``
21692169

21702170

21712171

Doc/whatsnew/3.11.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,12 @@ Removed
574574
because it was not used and added by mistake in previous versions.
575575
(Contributed by Nikita Sobolev in :issue:`46483`.)
576576

577+
* Remove the undocumented private ``float.__set_format__()`` method, previously
578+
known as ``float.__setformat__()`` in Python 3.7. Its docstring said: "You
579+
probably don't want to use this function. It exists mainly to be used in
580+
Python's test suite."
581+
(Contributed by Victor Stinner in :issue:`46852`.)
582+
577583
Porting to Python 3.11
578584
======================
579585

Lib/test/test_float.py

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
have_getformat = hasattr(float, "__getformat__")
2020
requires_getformat = unittest.skipUnless(have_getformat,
2121
"requires __getformat__")
22-
requires_setformat = unittest.skipUnless(hasattr(float, "__setformat__"),
23-
"requires __setformat__")
2422

2523
#locate file with float format test values
2624
test_dir = os.path.dirname(__file__) or os.curdir
@@ -612,44 +610,6 @@ class F(float, H):
612610
self.assertEqual(hash(value), object.__hash__(value))
613611

614612

615-
@requires_setformat
616-
class FormatFunctionsTestCase(unittest.TestCase):
617-
618-
def setUp(self):
619-
self.save_formats = {'double':float.__getformat__('double'),
620-
'float':float.__getformat__('float')}
621-
622-
def tearDown(self):
623-
float.__setformat__('double', self.save_formats['double'])
624-
float.__setformat__('float', self.save_formats['float'])
625-
626-
def test_getformat(self):
627-
self.assertIn(float.__getformat__('double'),
628-
['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
629-
self.assertIn(float.__getformat__('float'),
630-
['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
631-
self.assertRaises(ValueError, float.__getformat__, 'chicken')
632-
self.assertRaises(TypeError, float.__getformat__, 1)
633-
634-
def test_setformat(self):
635-
for t in 'double', 'float':
636-
float.__setformat__(t, 'unknown')
637-
if self.save_formats[t] == 'IEEE, big-endian':
638-
self.assertRaises(ValueError, float.__setformat__,
639-
t, 'IEEE, little-endian')
640-
elif self.save_formats[t] == 'IEEE, little-endian':
641-
self.assertRaises(ValueError, float.__setformat__,
642-
t, 'IEEE, big-endian')
643-
else:
644-
self.assertRaises(ValueError, float.__setformat__,
645-
t, 'IEEE, big-endian')
646-
self.assertRaises(ValueError, float.__setformat__,
647-
t, 'IEEE, little-endian')
648-
self.assertRaises(ValueError, float.__setformat__,
649-
t, 'chicken')
650-
self.assertRaises(ValueError, float.__setformat__,
651-
'chicken', 'unknown')
652-
653613
BE_DOUBLE_INF = b'\x7f\xf0\x00\x00\x00\x00\x00\x00'
654614
LE_DOUBLE_INF = bytes(reversed(BE_DOUBLE_INF))
655615
BE_DOUBLE_NAN = b'\x7f\xf8\x00\x00\x00\x00\x00\x00'
@@ -660,36 +620,6 @@ def test_setformat(self):
660620
BE_FLOAT_NAN = b'\x7f\xc0\x00\x00'
661621
LE_FLOAT_NAN = bytes(reversed(BE_FLOAT_NAN))
662622

663-
# on non-IEEE platforms, attempting to unpack a bit pattern
664-
# representing an infinity or a NaN should raise an exception.
665-
666-
@requires_setformat
667-
class UnknownFormatTestCase(unittest.TestCase):
668-
def setUp(self):
669-
self.save_formats = {'double':float.__getformat__('double'),
670-
'float':float.__getformat__('float')}
671-
float.__setformat__('double', 'unknown')
672-
float.__setformat__('float', 'unknown')
673-
674-
def tearDown(self):
675-
float.__setformat__('double', self.save_formats['double'])
676-
float.__setformat__('float', self.save_formats['float'])
677-
678-
def test_double_specials_dont_unpack(self):
679-
for fmt, data in [('>d', BE_DOUBLE_INF),
680-
('>d', BE_DOUBLE_NAN),
681-
('<d', LE_DOUBLE_INF),
682-
('<d', LE_DOUBLE_NAN)]:
683-
self.assertRaises(ValueError, struct.unpack, fmt, data)
684-
685-
def test_float_specials_dont_unpack(self):
686-
for fmt, data in [('>f', BE_FLOAT_INF),
687-
('>f', BE_FLOAT_NAN),
688-
('<f', LE_FLOAT_INF),
689-
('<f', LE_FLOAT_NAN)]:
690-
self.assertRaises(ValueError, struct.unpack, fmt, data)
691-
692-
693623
# on an IEEE platform, all we guarantee is that bit patterns
694624
# representing infinities or NaNs do not raise an exception; all else
695625
# is accident (today).

Lib/unittest/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ def _patch_stopall():
19431943
_non_defaults = {
19441944
'__get__', '__set__', '__delete__', '__reversed__', '__missing__',
19451945
'__reduce__', '__reduce_ex__', '__getinitargs__', '__getnewargs__',
1946-
'__getstate__', '__setstate__', '__getformat__', '__setformat__',
1946+
'__getstate__', '__setstate__', '__getformat__',
19471947
'__repr__', '__dir__', '__subclasses__', '__format__',
19481948
'__getnewargs_ex__',
19491949
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Remove the undocumented private ``float.__set_format__()`` method, previously
2+
known as ``float.__setformat__()`` in Python 3.7. Its docstring said: "You
3+
probably don't want to use this function. It exists mainly to be used in
4+
Python's test suite." Patch by Victor Stinner.

Objects/clinic/floatobject.c.h

Lines changed: 1 addition & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/floatobject.c

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,6 @@ typedef enum {
17081708
} float_format_type;
17091709

17101710
static float_format_type double_format, float_format;
1711-
static float_format_type detected_double_format, detected_float_format;
17121711

17131712
/*[clinic input]
17141713
@classmethod
@@ -1760,78 +1759,6 @@ float___getformat___impl(PyTypeObject *type, const char *typestr)
17601759
}
17611760
}
17621761

1763-
/*[clinic input]
1764-
@classmethod
1765-
float.__setformat__
1766-
1767-
typestr: str
1768-
Must be 'double' or 'float'.
1769-
fmt: str
1770-
Must be one of 'unknown', 'IEEE, big-endian' or 'IEEE, little-endian',
1771-
and in addition can only be one of the latter two if it appears to
1772-
match the underlying C reality.
1773-
/
1774-
1775-
You probably don't want to use this function.
1776-
1777-
It exists mainly to be used in Python's test suite.
1778-
1779-
Override the automatic determination of C-level floating point type.
1780-
This affects how floats are converted to and from binary strings.
1781-
[clinic start generated code]*/
1782-
1783-
static PyObject *
1784-
float___setformat___impl(PyTypeObject *type, const char *typestr,
1785-
const char *fmt)
1786-
/*[clinic end generated code: output=06864de1fb5f1f04 input=c0e9e04dd87f9988]*/
1787-
{
1788-
float_format_type f;
1789-
float_format_type detected;
1790-
float_format_type *p;
1791-
1792-
if (strcmp(typestr, "double") == 0) {
1793-
p = &double_format;
1794-
detected = detected_double_format;
1795-
}
1796-
else if (strcmp(typestr, "float") == 0) {
1797-
p = &float_format;
1798-
detected = detected_float_format;
1799-
}
1800-
else {
1801-
PyErr_SetString(PyExc_ValueError,
1802-
"__setformat__() argument 1 must "
1803-
"be 'double' or 'float'");
1804-
return NULL;
1805-
}
1806-
1807-
if (strcmp(fmt, "unknown") == 0) {
1808-
f = unknown_format;
1809-
}
1810-
else if (strcmp(fmt, "IEEE, little-endian") == 0) {
1811-
f = ieee_little_endian_format;
1812-
}
1813-
else if (strcmp(fmt, "IEEE, big-endian") == 0) {
1814-
f = ieee_big_endian_format;
1815-
}
1816-
else {
1817-
PyErr_SetString(PyExc_ValueError,
1818-
"__setformat__() argument 2 must be "
1819-
"'unknown', 'IEEE, little-endian' or "
1820-
"'IEEE, big-endian'");
1821-
return NULL;
1822-
1823-
}
1824-
1825-
if (f != unknown_format && f != detected) {
1826-
PyErr_Format(PyExc_ValueError,
1827-
"can only set %s format to 'unknown' or the "
1828-
"detected platform value", typestr);
1829-
return NULL;
1830-
}
1831-
1832-
*p = f;
1833-
Py_RETURN_NONE;
1834-
}
18351762

18361763
static PyObject *
18371764
float_getreal(PyObject *v, void *closure)
@@ -1885,7 +1812,6 @@ static PyMethodDef float_methods[] = {
18851812
FLOAT_IS_INTEGER_METHODDEF
18861813
FLOAT___GETNEWARGS___METHODDEF
18871814
FLOAT___GETFORMAT___METHODDEF
1888-
FLOAT___SETFORMAT___METHODDEF
18891815
FLOAT___FORMAT___METHODDEF
18901816
{NULL, NULL} /* sentinel */
18911817
};
@@ -1989,6 +1915,8 @@ _PyFloat_InitState(PyInterpreterState *interp)
19891915
return;
19901916
}
19911917

1918+
float_format_type detected_double_format, detected_float_format;
1919+
19921920
/* We attempt to determine if this machine is using IEEE
19931921
floating point formats by peering at the bits of some
19941922
carefully chosen values. If it looks like we are on an

0 commit comments

Comments
 (0)