Skip to content

Commit 31a7552

Browse files
eendebakptclaude
andcommitted
Avoid a reference count round trip when converting int arguments to Py_ssize_t
Add _PyNumber_IndexAsSsize_t() and use it in Argument Clinic and in the 'n' format unit of PyArg_Parse. Also add a fast path for int to PyNumber_AsSsize_t(). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent ff9803a commit 31a7552

51 files changed

Lines changed: 744 additions & 1945 deletions

Some content is hidden

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

‎Include/internal/pycore_abstract.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ PyAPI_FUNC(int) _Py_convert_optional_to_non_negative_ssize_t(PyObject *, void *)
6060
// Export for 'math' shared extension.
6161
PyAPI_FUNC(PyObject*) _PyNumber_Index(PyObject *o);
6262

63+
PyAPI_FUNC(Py_ssize_t) _PyNumber_IndexAsSsize_t(PyObject *o);
64+
6365
typedef struct {
6466
PyObject *object;
6567
PySendResult kind;

‎Lib/test/clinic.test.c‎

Lines changed: 18 additions & 50 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_IndexAsSsize_t(args[0]);
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_IndexAsSsize_t(args[1]);
1663+
if (b == -1 && PyErr_Occurred()) {
1664+
goto exit;
16811665
}
16821666
if (nargs < 3) {
16831667
goto skip_optional;
@@ -1688,22 +1672,14 @@ 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;
1702-
if (d < 0) {
1703-
PyErr_SetString(PyExc_ValueError,
1704-
"d cannot be negative");
1705-
goto exit;
1706-
}
1675+
d = _PyNumber_IndexAsSsize_t(args[3]);
1676+
if (d == -1 && PyErr_Occurred()) {
1677+
goto exit;
1678+
}
1679+
if (d < 0) {
1680+
PyErr_SetString(PyExc_ValueError,
1681+
"d cannot be negative");
1682+
goto exit;
17071683
}
17081684
if (nargs < 5) {
17091685
goto skip_optional;
@@ -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_IndexAsSsize_t(args[5]);
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=e4286b4ef87a92ab input=a25bac8ecf2890aa]*/
17471715

17481716

17491717
/*[clinic input]

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

Lines changed: 18 additions & 34 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: 8 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: 11 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)