Skip to content

Commit 7945e61

Browse files
committed
gh-150942: Speed up csv.reader row building
1 parent 2f064fb commit 7945e61

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Speed up :func:`csv.reader` by appending parsed fields to the output row
2+
without an extra reference-count round-trip (using the internal
3+
reference-stealing list append helper).

Modules/_csv.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module instead.
1414
#endif
1515

1616
#include "Python.h"
17+
#include "pycore_list.h" // _PyList_AppendTakeRef()
1718
#include "pycore_pyatomic_ft_wrappers.h"
1819

1920
#include <stddef.h> // offsetof()
@@ -685,11 +686,9 @@ parse_save_field(ReaderObj *self)
685686
}
686687
self->field_len = 0;
687688
}
688-
if (PyList_Append(self->fields, field) < 0) {
689-
Py_DECREF(field);
689+
if (_PyList_AppendTakeRef((PyListObject *)self->fields, field) < 0) {
690690
return -1;
691691
}
692-
Py_DECREF(field);
693692
return 0;
694693
}
695694

0 commit comments

Comments
 (0)