Skip to content
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

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
25 changes: 18 additions & 7 deletions Modules/_csv.c
Expand Up @@ -34,7 +34,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 */
aisk marked this conversation as resolved.
Show resolved Hide resolved
PyObject *str_write;
} _csvstate;

Expand Down Expand Up @@ -702,10 +702,15 @@ 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) {
#ifdef Py_GIL_DISABLED
uint32_t field_limit = _Py_atomic_load_int32(&module_state->field_limit);
aisk marked this conversation as resolved.
Show resolved Hide resolved
#else
uint32_t field_limit = module_state->field_limit;
#endif
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 +1576,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 +1588,10 @@ 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) {
Py_BEGIN_CRITICAL_SECTION(module_state->dialects);
res = PyDict_SetItem(module_state->dialects, name_obj, dialect);
aisk marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we don't have to use res anymore. :)

Py_END_CRITICAL_SECTION();
if (res < 0) {
Py_DECREF(dialect);
return NULL;
}
Expand All @@ -1592,6 +1601,7 @@ csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs)


/*[clinic input]
@critical_section
_csv.unregister_dialect

name: object
Expand All @@ -1603,7 +1613,7 @@ Delete the name/dialect mapping associated with a string name.

static PyObject *
_csv_unregister_dialect_impl(PyObject *module, PyObject *name)
/*[clinic end generated code: output=0813ebca6c058df4 input=6b5c1557bf60c7e7]*/
/*[clinic end generated code: output=0813ebca6c058df4 input=c38732b506218713]*/
{
_csvstate *module_state = get_csv_state(module);
int rc = PyDict_Pop(module_state->dialects, name, NULL);
Expand Down Expand Up @@ -1635,6 +1645,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 +1660,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
7 changes: 6 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.