Skip to content

gh-116738: Make _csv module thread-safe #118344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/internal/pycore_pyatomic_ft_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern "C" {
_Py_atomic_store_ssize_relaxed(&value, new_value)
#define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) \
_Py_atomic_store_uint8_relaxed(&value, new_value)
#define FT_ATOMIC_LOAD_INT32(value) _Py_atomic_load_int32(&value)

#else
#define FT_ATOMIC_LOAD_PTR(value) value
Expand All @@ -53,6 +54,7 @@ extern "C" {
#define FT_ATOMIC_STORE_UINTPTR_RELEASE(value, new_value) value = new_value
#define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_LOAD_INT32(value) value

#endif

Expand Down
17 changes: 11 additions & 6 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module instead.
#endif

#include "Python.h"
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_INT32

#include <stddef.h> // offsetof()
#include <stdbool.h>
Expand All @@ -34,7 +35,7 @@ typedef struct {
PyTypeObject *dialect_type;
PyTypeObject *reader_type;
PyTypeObject *writer_type;
long field_limit; /* max parsed field size */
int32_t field_limit; /* max parsed field size */
PyObject *str_write;
} _csvstate;

Expand Down Expand Up @@ -702,10 +703,11 @@ parse_grow_buff(ReaderObj *self)
static int
parse_add_char(ReaderObj *self, _csvstate *module_state, Py_UCS4 c)
{
if (self->field_len >= module_state->field_limit) {
uint32_t field_limit = FT_ATOMIC_LOAD_INT32(module_state->field_limit);
if (self->field_len >= field_limit) {
PyErr_Format(module_state->error_obj,
"field larger than field limit (%ld)",
module_state->field_limit);
field_limit);
return -1;
}
if (self->field_len == self->field_size && !parse_grow_buff(self))
Expand Down Expand Up @@ -1571,6 +1573,7 @@ csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *name_obj, *dialect_obj = NULL;
_csvstate *module_state = get_csv_state(module);
PyObject *dialect;
int res;

if (!PyArg_UnpackTuple(args, "", 1, 2, &name_obj, &dialect_obj))
return NULL;
Expand All @@ -1582,7 +1585,8 @@ csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs)
dialect = _call_dialect(module_state, dialect_obj, kwargs);
if (dialect == NULL)
return NULL;
if (PyDict_SetItem(module_state->dialects, name_obj, dialect) < 0) {
res = PyDict_SetItem(module_state->dialects, name_obj, dialect);
if (res < 0) {
Py_DECREF(dialect);
return NULL;
}
Expand Down Expand Up @@ -1635,6 +1639,7 @@ _csv_get_dialect_impl(PyObject *module, PyObject *name)
}

/*[clinic input]
@critical_section
_csv.field_size_limit

new_limit: object = NULL
Expand All @@ -1649,10 +1654,10 @@ the old limit is returned

static PyObject *
_csv_field_size_limit_impl(PyObject *module, PyObject *new_limit)
/*[clinic end generated code: output=f2799ecd908e250b input=cec70e9226406435]*/
/*[clinic end generated code: output=f2799ecd908e250b input=3e49d42e37a7d449]*/
{
_csvstate *module_state = get_csv_state(module);
long old_limit = module_state->field_limit;
int32_t old_limit = module_state->field_limit;
if (new_limit != NULL) {
if (!PyLong_CheckExact(new_limit)) {
PyErr_Format(PyExc_TypeError,
Expand Down
5 changes: 4 additions & 1 deletion Modules/clinic/_csv.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.