Skip to content

Commit 1adc837

Browse files
authored
bpo-40676: Use Argument Clinic for csv (where possible) (GH-20200)
1 parent 055760e commit 1adc837

File tree

3 files changed

+200
-37
lines changed

3 files changed

+200
-37
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Convert :mod:`csv` to use Argument Clinic for :func:`csv.field_size_limit`,
2+
:func:`csv.get_dialect`, :func:`csv.unregister_dialect` and :func:`csv.list_dialects`.
3+

Modules/_csv.c

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ module instead.
1414
#include "structmember.h" // PyMemberDef
1515
#include <stdbool.h>
1616

17+
/*[clinic input]
18+
module _csv
19+
[clinic start generated code]*/
20+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=385118b71aa43706]*/
21+
22+
#include "clinic/_csv.c.h"
1723
#define NOT_SET ((Py_UCS4)-1)
1824
#define EOL ((Py_UCS4)-2)
1925

@@ -1473,8 +1479,18 @@ csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
14731479
/*
14741480
* DIALECT REGISTRY
14751481
*/
1482+
1483+
/*[clinic input]
1484+
_csv.list_dialects
1485+
1486+
Return a list of all known dialect names.
1487+
1488+
names = csv.list_dialects()
1489+
[clinic start generated code]*/
1490+
14761491
static PyObject *
1477-
csv_list_dialects(PyObject *module, PyObject *args)
1492+
_csv_list_dialects_impl(PyObject *module)
1493+
/*[clinic end generated code: output=a5b92b215b006a6d input=8953943eb17d98ab]*/
14781494
{
14791495
return PyDict_Keys(get_csv_state(module)->dialects);
14801496
}
@@ -1506,11 +1522,23 @@ csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs)
15061522
Py_RETURN_NONE;
15071523
}
15081524

1525+
1526+
/*[clinic input]
1527+
_csv.unregister_dialect
1528+
1529+
name: object
1530+
1531+
Delete the name/dialect mapping associated with a string name.
1532+
1533+
csv.unregister_dialect(name)
1534+
[clinic start generated code]*/
1535+
15091536
static PyObject *
1510-
csv_unregister_dialect(PyObject *module, PyObject *name_obj)
1537+
_csv_unregister_dialect_impl(PyObject *module, PyObject *name)
1538+
/*[clinic end generated code: output=0813ebca6c058df4 input=6b5c1557bf60c7e7]*/
15111539
{
15121540
_csvstate *module_state = get_csv_state(module);
1513-
if (PyDict_DelItem(module_state->dialects, name_obj) < 0) {
1541+
if (PyDict_DelItem(module_state->dialects, name) < 0) {
15141542
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
15151543
PyErr_Format(module_state->error_obj, "unknown dialect");
15161544
}
@@ -1519,21 +1547,42 @@ csv_unregister_dialect(PyObject *module, PyObject *name_obj)
15191547
Py_RETURN_NONE;
15201548
}
15211549

1550+
/*[clinic input]
1551+
_csv.get_dialect
1552+
1553+
name: object
1554+
1555+
Return the dialect instance associated with name.
1556+
1557+
dialect = csv.get_dialect(name)
1558+
[clinic start generated code]*/
1559+
15221560
static PyObject *
1523-
csv_get_dialect(PyObject *module, PyObject *name_obj)
1561+
_csv_get_dialect_impl(PyObject *module, PyObject *name)
1562+
/*[clinic end generated code: output=aa988cd573bebebb input=edf9ddab32e448fb]*/
15241563
{
1525-
return get_dialect_from_registry(name_obj, get_csv_state(module));
1564+
return get_dialect_from_registry(name, get_csv_state(module));
15261565
}
15271566

1567+
/*[clinic input]
1568+
_csv.field_size_limit
1569+
1570+
new_limit: object = NULL
1571+
1572+
Sets an upper limit on parsed fields.
1573+
1574+
csv.field_size_limit([limit])
1575+
1576+
Returns old limit. If limit is not given, no new limit is set and
1577+
the old limit is returned
1578+
[clinic start generated code]*/
1579+
15281580
static PyObject *
1529-
csv_field_size_limit(PyObject *module, PyObject *args)
1581+
_csv_field_size_limit_impl(PyObject *module, PyObject *new_limit)
1582+
/*[clinic end generated code: output=f2799ecd908e250b input=cec70e9226406435]*/
15301583
{
1531-
PyObject *new_limit = NULL;
15321584
_csvstate *module_state = get_csv_state(module);
15331585
long old_limit = module_state->field_limit;
1534-
1535-
if (!PyArg_UnpackTuple(args, "field_size_limit", 0, 1, &new_limit))
1536-
return NULL;
15371586
if (new_limit != NULL) {
15381587
if (!PyLong_CheckExact(new_limit)) {
15391588
PyErr_Format(PyExc_TypeError,
@@ -1650,44 +1699,21 @@ PyDoc_STRVAR(csv_writer_doc,
16501699
"\n"
16511700
"The \"fileobj\" argument can be any object that supports the file API.\n");
16521701

1653-
PyDoc_STRVAR(csv_list_dialects_doc,
1654-
"Return a list of all know dialect names.\n"
1655-
" names = csv.list_dialects()");
1656-
1657-
PyDoc_STRVAR(csv_get_dialect_doc,
1658-
"Return the dialect instance associated with name.\n"
1659-
" dialect = csv.get_dialect(name)");
1660-
16611702
PyDoc_STRVAR(csv_register_dialect_doc,
16621703
"Create a mapping from a string name to a dialect class.\n"
16631704
" dialect = csv.register_dialect(name[, dialect[, **fmtparams]])");
16641705

1665-
PyDoc_STRVAR(csv_unregister_dialect_doc,
1666-
"Delete the name/dialect mapping associated with a string name.\n"
1667-
" csv.unregister_dialect(name)");
1668-
1669-
PyDoc_STRVAR(csv_field_size_limit_doc,
1670-
"Sets an upper limit on parsed fields.\n"
1671-
" csv.field_size_limit([limit])\n"
1672-
"\n"
1673-
"Returns old limit. If limit is not given, no new limit is set and\n"
1674-
"the old limit is returned");
1675-
16761706
static struct PyMethodDef csv_methods[] = {
16771707
{ "reader", (PyCFunction)(void(*)(void))csv_reader,
16781708
METH_VARARGS | METH_KEYWORDS, csv_reader_doc},
16791709
{ "writer", (PyCFunction)(void(*)(void))csv_writer,
16801710
METH_VARARGS | METH_KEYWORDS, csv_writer_doc},
1681-
{ "list_dialects", (PyCFunction)csv_list_dialects,
1682-
METH_NOARGS, csv_list_dialects_doc},
16831711
{ "register_dialect", (PyCFunction)(void(*)(void))csv_register_dialect,
16841712
METH_VARARGS | METH_KEYWORDS, csv_register_dialect_doc},
1685-
{ "unregister_dialect", (PyCFunction)csv_unregister_dialect,
1686-
METH_O, csv_unregister_dialect_doc},
1687-
{ "get_dialect", (PyCFunction)csv_get_dialect,
1688-
METH_O, csv_get_dialect_doc},
1689-
{ "field_size_limit", (PyCFunction)csv_field_size_limit,
1690-
METH_VARARGS, csv_field_size_limit_doc},
1713+
_CSV_LIST_DIALECTS_METHODDEF
1714+
_CSV_UNREGISTER_DIALECT_METHODDEF
1715+
_CSV_GET_DIALECT_METHODDEF
1716+
_CSV_FIELD_SIZE_LIMIT_METHODDEF
16911717
{ NULL, NULL }
16921718
};
16931719

Modules/clinic/_csv.c.h

Lines changed: 134 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)