From a2bfdd6900e9bd1f0ed4d3b24b3b6d5bce5e4ad1 Mon Sep 17 00:00:00 2001 From: Abdullah Masood Date: Mon, 17 Aug 2026 17:46:14 +0500 Subject: [PATCH] gh-150942: Improve performance of csv reader by using _PyList_AppendTakeRef --- .../C_API/2026-08-17-17-46-00.gh-issue-150942.abcdef.rst | 1 + Modules/_csv.c | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/C_API/2026-08-17-17-46-00.gh-issue-150942.abcdef.rst diff --git a/Misc/NEWS.d/next/C_API/2026-08-17-17-46-00.gh-issue-150942.abcdef.rst b/Misc/NEWS.d/next/C_API/2026-08-17-17-46-00.gh-issue-150942.abcdef.rst new file mode 100644 index 000000000000000..7d060ebd375e3cf --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-08-17-17-46-00.gh-issue-150942.abcdef.rst @@ -0,0 +1 @@ +Improve performance of ``csv`` reader by using ``_PyList_AppendTakeRef`` internally. diff --git a/Modules/_csv.c b/Modules/_csv.c index a7fcc78e058f058..cddc09c3b23ee82 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -15,6 +15,7 @@ module instead. #include "Python.h" #include "pycore_pyatomic_ft_wrappers.h" +#include "pycore_list.h" #include // offsetof() #include @@ -685,11 +686,9 @@ parse_save_field(ReaderObj *self) } self->field_len = 0; } - if (PyList_Append(self->fields, field) < 0) { - Py_DECREF(field); + if (_PyList_AppendTakeRef((PyListObject *)self->fields, field) < 0) { return -1; } - Py_DECREF(field); return 0; }