diff -r f44f44b14dfc Modules/_csv.c --- a/Modules/_csv.c Fri Jan 20 08:35:18 2017 +0200 +++ b/Modules/_csv.c Fri Jan 20 18:17:14 2017 +0200 @@ -13,6 +13,12 @@ module instead. #include "Python.h" #include "structmember.h" +#include "clinic/_csv.c.h" +/*[clinic input] +module _csv +class _csv.Dialect "PyObject *" "&Dialect_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6f3170c88d1c5a7a]*/ typedef struct { PyObject *error_obj; /* CSV exception */ @@ -1421,12 +1427,21 @@ csv_writer(PyObject *module, PyObject *a /* * DIALECT REGISTRY */ + +/*[clinic input] +_csv.list_dialects + +Return a list of all know dialect names. +[clinic start generated code]*/ + static PyObject * -csv_list_dialects(PyObject *module, PyObject *args) +_csv_list_dialects_impl(PyObject *module) +/*[clinic end generated code: output=a5b92b215b006a6d input=a31ccb1071637d7c]*/ { return PyDict_Keys(_csvstate_global->dialects); } + static PyObject * csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs) { @@ -1454,36 +1469,69 @@ csv_register_dialect(PyObject *module, P return Py_None; } +/*[clinic input] +_csv.unregister_dialect + + name: object + / + +Delete the name/dialect mapping associated with a string name. +[clinic start generated code]*/ + static PyObject * -csv_unregister_dialect(PyObject *module, PyObject *name_obj) +_csv_unregister_dialect(PyObject *module, PyObject *name) +/*[clinic end generated code: output=cf44401586cd32eb input=24474725768445c0]*/ { - if (PyDict_DelItem(_csvstate_global->dialects, name_obj) < 0) + if (PyDict_DelItem(_csvstate_global->dialects, name) < 0) return PyErr_Format(_csvstate_global->error_obj, "unknown dialect"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } + +/*[clinic input] +_csv.get_dialect + + name: object + / + +Return the dialect instance associated with name. +[clinic start generated code]*/ + static PyObject * -csv_get_dialect(PyObject *module, PyObject *name_obj) +_csv_get_dialect(PyObject *module, PyObject *name) +/*[clinic end generated code: output=8f66fc0a27b68e24 input=c97af3b62e402afa]*/ { - return get_dialect_from_registry(name_obj); + return get_dialect_from_registry(name); } + +/*[clinic input] +_csv.field_size_limit + + [ + limit: object + ] + / + +Sets an upper limit on parsed fields. + +Returns old limit. If limit is not given, no new limit is set and +the old limit is returned. +[clinic start generated code]*/ + static PyObject * -csv_field_size_limit(PyObject *module, PyObject *args) +_csv_field_size_limit(PyObject *module, int group_right_1, PyObject *limit) +/*[clinic end generated code: output=49e0056f0b1e96f7 input=76b66600a7195ac4]*/ { - PyObject *new_limit = NULL; long old_limit = _csvstate_global->field_limit; - if (!PyArg_UnpackTuple(args, "field_size_limit", 0, 1, &new_limit)) - return NULL; - if (new_limit != NULL) { - if (!PyLong_CheckExact(new_limit)) { + if (group_right_1) { + if (!PyLong_CheckExact(limit)) { PyErr_Format(PyExc_TypeError, "limit must be an integer"); return NULL; } - _csvstate_global->field_limit = PyLong_AsLong(new_limit); + _csvstate_global->field_limit = PyLong_AsLong(limit); if (_csvstate_global->field_limit == -1 && PyErr_Occurred()) { _csvstate_global->field_limit = old_limit; return NULL; @@ -1583,44 +1631,22 @@ PyDoc_STRVAR(csv_writer_doc, "\n" "The \"fileobj\" argument can be any object that supports the file API.\n"); -PyDoc_STRVAR(csv_list_dialects_doc, -"Return a list of all know dialect names.\n" -" names = csv.list_dialects()"); - -PyDoc_STRVAR(csv_get_dialect_doc, -"Return the dialect instance associated with name.\n" -" dialect = csv.get_dialect(name)"); - PyDoc_STRVAR(csv_register_dialect_doc, "Create a mapping from a string name to a dialect class.\n" " dialect = csv.register_dialect(name[, dialect[, **fmtparams]])"); -PyDoc_STRVAR(csv_unregister_dialect_doc, -"Delete the name/dialect mapping associated with a string name.\n" -" csv.unregister_dialect(name)"); - -PyDoc_STRVAR(csv_field_size_limit_doc, -"Sets an upper limit on parsed fields.\n" -" csv.field_size_limit([limit])\n" -"\n" -"Returns old limit. If limit is not given, no new limit is set and\n" -"the old limit is returned"); static struct PyMethodDef csv_methods[] = { { "reader", (PyCFunction)csv_reader, METH_VARARGS | METH_KEYWORDS, csv_reader_doc}, { "writer", (PyCFunction)csv_writer, METH_VARARGS | METH_KEYWORDS, csv_writer_doc}, - { "list_dialects", (PyCFunction)csv_list_dialects, - METH_NOARGS, csv_list_dialects_doc}, { "register_dialect", (PyCFunction)csv_register_dialect, - METH_VARARGS | METH_KEYWORDS, csv_register_dialect_doc}, - { "unregister_dialect", (PyCFunction)csv_unregister_dialect, - METH_O, csv_unregister_dialect_doc}, - { "get_dialect", (PyCFunction)csv_get_dialect, - METH_O, csv_get_dialect_doc}, - { "field_size_limit", (PyCFunction)csv_field_size_limit, - METH_VARARGS, csv_field_size_limit_doc}, + METH_VARARGS | METH_KEYWORDS, csv_register_dialect_doc}, + _CSV_LIST_DIALECTS_METHODDEF + _CSV_UNREGISTER_DIALECT_METHODDEF + _CSV_GET_DIALECT_METHODDEF + _CSV_FIELD_SIZE_LIMIT_METHODDEF { NULL, NULL } }; diff -r f44f44b14dfc Modules/clinic/_csv.c.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Modules/clinic/_csv.c.h Fri Jan 20 18:17:14 2017 +0200 @@ -0,0 +1,50 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +PyDoc_STRVAR(_csv_list_dialects__doc__, +"list_dialects($module, /)\n" +"--\n" +"\n" +"Return a list of all know dialect names."); + +#define _CSV_LIST_DIALECTS_METHODDEF \ + {"list_dialects", (PyCFunction)_csv_list_dialects, METH_NOARGS, _csv_list_dialects__doc__}, + +static PyObject * +_csv_list_dialects_impl(PyObject *module); + +static PyObject * +_csv_list_dialects(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return _csv_list_dialects_impl(module); +} + +PyDoc_STRVAR(_csv_unregister_dialect__doc__, +"unregister_dialect($module, name, /)\n" +"--\n" +"\n" +"Delete the name/dialect mapping associated with a string name."); + +#define _CSV_UNREGISTER_DIALECT_METHODDEF \ + {"unregister_dialect", (PyCFunction)_csv_unregister_dialect, METH_O, _csv_unregister_dialect__doc__}, + +PyDoc_STRVAR(_csv_get_dialect__doc__, +"get_dialect($module, name, /)\n" +"--\n" +"\n" +"Return the dialect instance associated with name."); + +#define _CSV_GET_DIALECT_METHODDEF \ + {"get_dialect", (PyCFunction)_csv_get_dialect, METH_O, _csv_get_dialect__doc__}, + +PyDoc_STRVAR(_csv_field_size_limit__doc__, +"field_size_limit([limit])\n" +"Sets an upper limit on parsed fields.\n" +"\n" +"Returns old limit. If limit is not given, no new limit is set and\n" +"the old limit is returned."); + +#define _CSV_FIELD_SIZE_LIMIT_METHODDEF \ + {"field_size_limit", (PyCFunction)_csv_field_size_limit, METH_O, _csv_field_size_limit__doc__}, +/*[clinic end generated code: output=ce2035869742b7db input=a9049054013a1b77]*/