Skip to content

Commit 203e49a

Browse files
authored
gh-162749: Improve AC generated code for conversion of int to Py_ssize_t (#162750)
gh-162749: Improve AC generated code for conversion of int to Py_ssize_t. This avoids an incref/decref pair in the common case of compact int arguments.
1 parent 0e671f2 commit 203e49a

57 files changed

Lines changed: 560 additions & 1852 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Lib/test/clinic.test.c‎

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,32 +1652,16 @@ test_Py_ssize_t_converter(PyObject *module, PyObject *const *args, Py_ssize_t na
16521652
if (nargs < 1) {
16531653
goto skip_optional;
16541654
}
1655-
{
1656-
Py_ssize_t ival = -1;
1657-
PyObject *iobj = _PyNumber_Index(args[0]);
1658-
if (iobj != NULL) {
1659-
ival = PyLong_AsSsize_t(iobj);
1660-
Py_DECREF(iobj);
1661-
}
1662-
if (ival == -1 && PyErr_Occurred()) {
1663-
goto exit;
1664-
}
1665-
a = ival;
1655+
a = PyNumber_AsSsize_t(args[0], PyExc_OverflowError);
1656+
if (a == -1 && PyErr_Occurred()) {
1657+
goto exit;
16661658
}
16671659
if (nargs < 2) {
16681660
goto skip_optional;
16691661
}
1670-
{
1671-
Py_ssize_t ival = -1;
1672-
PyObject *iobj = _PyNumber_Index(args[1]);
1673-
if (iobj != NULL) {
1674-
ival = PyLong_AsSsize_t(iobj);
1675-
Py_DECREF(iobj);
1676-
}
1677-
if (ival == -1 && PyErr_Occurred()) {
1678-
goto exit;
1679-
}
1680-
b = ival;
1662+
b = PyNumber_AsSsize_t(args[1], PyExc_OverflowError);
1663+
if (b == -1 && PyErr_Occurred()) {
1664+
goto exit;
16811665
}
16821666
if (nargs < 3) {
16831667
goto skip_optional;
@@ -1688,23 +1672,15 @@ test_Py_ssize_t_converter(PyObject *module, PyObject *const *args, Py_ssize_t na
16881672
if (nargs < 4) {
16891673
goto skip_optional;
16901674
}
1691-
{
1692-
Py_ssize_t ival = -1;
1693-
PyObject *iobj = _PyNumber_Index(args[3]);
1694-
if (iobj != NULL) {
1695-
ival = PyLong_AsSsize_t(iobj);
1696-
Py_DECREF(iobj);
1697-
}
1698-
if (ival == -1 && PyErr_Occurred()) {
1699-
goto exit;
1700-
}
1701-
d = ival;
1675+
d = PyNumber_AsSsize_t(args[3], PyExc_OverflowError);
1676+
if (d == -1 && PyErr_Occurred()) {
1677+
goto exit;
1678+
}
17021679
if (d < 0) {
17031680
PyErr_SetString(PyExc_ValueError,
17041681
"d cannot be negative");
17051682
goto exit;
17061683
}
1707-
}
17081684
if (nargs < 5) {
17091685
goto skip_optional;
17101686
}
@@ -1714,17 +1690,9 @@ test_Py_ssize_t_converter(PyObject *module, PyObject *const *args, Py_ssize_t na
17141690
if (nargs < 6) {
17151691
goto skip_optional;
17161692
}
1717-
{
1718-
Py_ssize_t ival = -1;
1719-
PyObject *iobj = _PyNumber_Index(args[5]);
1720-
if (iobj != NULL) {
1721-
ival = PyLong_AsSsize_t(iobj);
1722-
Py_DECREF(iobj);
1723-
}
1724-
if (ival == -1 && PyErr_Occurred()) {
1725-
goto exit;
1726-
}
1727-
f = ival;
1693+
f = PyNumber_AsSsize_t(args[5], PyExc_OverflowError);
1694+
if (f == -1 && PyErr_Occurred()) {
1695+
goto exit;
17281696
}
17291697
if (nargs < 7) {
17301698
goto skip_optional;
@@ -1743,7 +1711,7 @@ static PyObject *
17431711
test_Py_ssize_t_converter_impl(PyObject *module, Py_ssize_t a, Py_ssize_t b,
17441712
Py_ssize_t c, Py_ssize_t d, Py_ssize_t e,
17451713
Py_ssize_t f, Py_ssize_t g)
1746-
/*[clinic end generated code: output=4ae0a56a1447fba9 input=a25bac8ecf2890aa]*/
1714+
/*[clinic end generated code: output=4f744bf0d897e01c input=a25bac8ecf2890aa]*/
17471715

17481716

17491717
/*[clinic input]

‎Lib/test/test_zlib.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,11 @@ def test_decompressobj_badflush(self):
337337

338338
@support.cpython_only
339339
def test_overflow(self):
340-
with self.assertRaisesRegex(OverflowError, 'int too large'):
340+
with self.assertRaisesRegex(OverflowError, "cannot fit 'int'"):
341341
zlib.decompress(b'', 15, sys.maxsize + 1)
342-
with self.assertRaisesRegex(OverflowError, 'int too large'):
342+
with self.assertRaisesRegex(OverflowError, "cannot fit 'int'"):
343343
zlib.decompressobj().decompress(b'', sys.maxsize + 1)
344-
with self.assertRaisesRegex(OverflowError, 'int too large'):
344+
with self.assertRaisesRegex(OverflowError, "cannot fit 'int'"):
345345
zlib.decompressobj().flush(sys.maxsize + 1)
346346

347347
@support.cpython_only
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Speed up conversion of :class:`int` arguments to sizes and indices. When
2+
the value does not fit, the :exc:`OverflowError` message is now ``cannot fit
3+
'int' into an index-sized integer`` instead of ``Python int too large to
4+
convert to C ssize_t``.

‎Modules/_ctypes/clinic/_ctypes.c.h‎

Lines changed: 9 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Modules/_ctypes/clinic/callproc.c.h‎

Lines changed: 7 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Modules/_ctypes/clinic/cfield.c.h‎

Lines changed: 10 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)