Skip to content

Commit 14ad9f0

Browse files
committed
hold strong references
1 parent c9ff412 commit 14ad9f0

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

‎Modules/_csv.c‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -496,19 +496,31 @@ dialect_new_impl(PyTypeObject *type, PyObject *dialect, PyObject *delimiter,
496496
PyObject *strict)
497497
/*[clinic end generated code: output=123393bdc71ddaa6 input=469f47d5890022f7]*/
498498
{
499-
DialectObj *self;
499+
DialectObj *self = NULL;
500500
PyObject *ret = NULL;
501501

502+
/* vectorcall passes borrowed references; hold strong references before
503+
get_dialect_from_registry() or tp_alloc() can run arbitrary Python. */
504+
Py_XINCREF(delimiter);
505+
Py_XINCREF(doublequote);
506+
Py_XINCREF(escapechar);
507+
Py_XINCREF(lineterminator);
508+
Py_XINCREF(quotechar);
509+
Py_XINCREF(quoting);
510+
Py_XINCREF(skipinitialspace);
511+
Py_XINCREF(strict);
512+
502513
_csvstate *module_state = _csv_state_from_type(type, "dialect_new");
503514
if (module_state == NULL) {
504-
return NULL;
515+
goto err;
505516
}
506517

507518
if (dialect != NULL) {
508519
if (PyUnicode_Check(dialect)) {
509520
dialect = get_dialect_from_registry(dialect, module_state);
510-
if (dialect == NULL)
511-
return NULL;
521+
if (dialect == NULL) {
522+
goto err;
523+
}
512524
}
513525
else
514526
Py_INCREF(dialect);
@@ -521,25 +533,17 @@ dialect_new_impl(PyTypeObject *type, PyObject *dialect, PyObject *delimiter,
521533
quotechar == NULL &&
522534
quoting == NULL &&
523535
skipinitialspace == NULL &&
524-
strict == NULL)
525-
return dialect;
536+
strict == NULL) {
537+
ret = Py_NewRef(dialect);
538+
goto err;
539+
}
526540
}
527541

528542
self = (DialectObj *)type->tp_alloc(type, 0);
529543
if (self == NULL) {
530-
Py_CLEAR(dialect);
531-
return NULL;
544+
goto err;
532545
}
533546
self->lineterminator = NULL;
534-
535-
Py_XINCREF(delimiter);
536-
Py_XINCREF(doublequote);
537-
Py_XINCREF(escapechar);
538-
Py_XINCREF(lineterminator);
539-
Py_XINCREF(quotechar);
540-
Py_XINCREF(quoting);
541-
Py_XINCREF(skipinitialspace);
542-
Py_XINCREF(strict);
543547
if (dialect != NULL) {
544548
#define DIALECT_GETATTR(v, n) \
545549
do { \

0 commit comments

Comments
 (0)