diff --git a/Misc/NEWS.d/next/Library/2026-06-05-12-00-00.gh-issue-150942.Hkdh87.rst b/Misc/NEWS.d/next/Library/2026-06-05-12-00-00.gh-issue-150942.Hkdh87.rst new file mode 100644 index 00000000000000..dd27f6f2616f16 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-05-12-00-00.gh-issue-150942.Hkdh87.rst @@ -0,0 +1,3 @@ +Speed up :func:`csv.reader` by appending parsed fields to the output row +without an extra reference-count round-trip (using the internal +reference-stealing list append helper). Patch by Omkar Kabde. diff --git a/Modules/_csv.c b/Modules/_csv.c index a7fcc78e058f05..118c96be7911e4 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -14,6 +14,7 @@ module instead. #endif #include "Python.h" +#include "pycore_list.h" // _PyList_AppendTakeRef() #include "pycore_pyatomic_ft_wrappers.h" #include // offsetof() @@ -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; }