diff -r 395e190ead36 Modules/_json.c --- a/Modules/_json.c Mon May 04 22:45:47 2015 -0400 +++ b/Modules/_json.c Tue May 05 09:02:26 2015 +0300 @@ -13,6 +13,13 @@ #define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType) #define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType) +/*[clinic input] +module _json +class _json.Scanner "PyScannerObject *" "&PyScannerType" +class _json.Encoder "PyEncoderObject *" "&PyEncoderType" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e61b95c44d13513]*/ + static PyTypeObject PyScannerType; static PyTypeObject PyEncoderType; @@ -63,6 +70,8 @@ static PyMemberDef encoder_members[] = { {NULL} }; +#include "clinic/_json.c.h" + static PyObject * join_list_unicode(PyObject *lst) { @@ -80,8 +89,6 @@ join_list_unicode(PyObject *lst) static PyObject * ascii_escape_unicode(PyObject *pystr); -static PyObject * -py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr); void init_json(void); static PyObject * scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr); @@ -567,86 +574,93 @@ bail: return NULL; } -PyDoc_STRVAR(pydoc_scanstring, - "scanstring(string, end, strict=True) -> (string, end)\n" - "\n" - "Scan the string s for a JSON string. End is the index of the\n" - "character in s after the quote that started the JSON string.\n" - "Unescapes all valid JSON string escape sequences and raises ValueError\n" - "on attempt to decode an invalid string. If strict is False then literal\n" - "control characters are allowed in the string.\n" - "\n" - "Returns a tuple of the decoded string and the index of the character in s\n" - "after the end quote." -); +/*[clinic input] +_json.scanstring + string: object + end: Py_ssize_t + strict: int(c_default="1") = True + / + +Scan the string for a JSON string. + +End is the index of the character in string after the quote that started +the JSON string. Unescapes all valid JSON string escape sequences and +raises ValueError on attempt to decode an invalid string. If strict is +False then literal control characters are allowed in the string. + +Returns a tuple of the decoded string and the index of the character in +string after the end quote. +[clinic start generated code]*/ static PyObject * -py_scanstring(PyObject* self UNUSED, PyObject *args) +_json_scanstring_impl(PyModuleDef *module, PyObject *string, Py_ssize_t end, + int strict) +/*[clinic end generated code: output=4480b2ceabe975bb input=dd1b6269c97cb8cf]*/ { - PyObject *pystr; PyObject *rval; - Py_ssize_t end; Py_ssize_t next_end = -1; - int strict = 1; - if (!PyArg_ParseTuple(args, "On|i:scanstring", &pystr, &end, &strict)) { - return NULL; - } - if (PyUnicode_Check(pystr)) { - rval = scanstring_unicode(pystr, end, strict, &next_end); + if (PyUnicode_Check(string)) { + rval = scanstring_unicode(string, end, strict, &next_end); } else { PyErr_Format(PyExc_TypeError, "first argument must be a string, not %.80s", - Py_TYPE(pystr)->tp_name); + Py_TYPE(string)->tp_name); return NULL; } return _build_rval_index_tuple(rval, next_end); } -PyDoc_STRVAR(pydoc_encode_basestring_ascii, - "encode_basestring_ascii(string) -> string\n" - "\n" - "Return an ASCII-only JSON representation of a Python string" -); +/*[clinic input] +_json.encode_basestring_ascii + string: object + / + +Return an ASCII-only JSON representation of a Python string. +[clinic start generated code]*/ static PyObject * -py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr) +_json_encode_basestring_ascii(PyModuleDef *module, PyObject *string) +/*[clinic end generated code: output=16a3ca3b7289c5db input=48005ab6e5829715]*/ { PyObject *rval; /* Return an ASCII-only JSON representation of a Python string */ /* METH_O */ - if (PyUnicode_Check(pystr)) { - rval = ascii_escape_unicode(pystr); + if (PyUnicode_Check(string)) { + rval = ascii_escape_unicode(string); } else { PyErr_Format(PyExc_TypeError, "first argument must be a string, not %.80s", - Py_TYPE(pystr)->tp_name); + Py_TYPE(string)->tp_name); return NULL; } return rval; } -PyDoc_STRVAR(pydoc_encode_basestring, - "encode_basestring(string) -> string\n" - "\n" - "Return a JSON representation of a Python string" -); +/*[clinic input] +_json.encode_basestring + string: object + / + +Return a JSON representation of a Python string. +[clinic start generated code]*/ static PyObject * -py_encode_basestring(PyObject* self UNUSED, PyObject *pystr) +_json_encode_basestring(PyModuleDef *module, PyObject *string) +/*[clinic end generated code: output=d9fd10e179f19bac input=2b9274a92a866079]*/ { PyObject *rval; /* Return a JSON representation of a Python string */ /* METH_O */ - if (PyUnicode_Check(pystr)) { - rval = escape_unicode(pystr); + if (PyUnicode_Check(string)) { + rval = escape_unicode(string); } else { PyErr_Format(PyExc_TypeError, "first argument must be a string, not %.80s", - Py_TYPE(pystr)->tp_name); + Py_TYPE(string)->tp_name); return NULL; } return rval; @@ -1348,8 +1362,8 @@ encoder_init(PyObject *self, PyObject *a s->fast_encode = NULL; if (PyCFunction_Check(s->encoder)) { PyCFunction f = PyCFunction_GetFunction(s->encoder); - if (f == (PyCFunction)py_encode_basestring_ascii || - f == (PyCFunction)py_encode_basestring) { + if (f == (PyCFunction)_json_encode_basestring_ascii || + f == (PyCFunction)_json_encode_basestring) { s->fast_encode = f; } } @@ -1940,18 +1954,9 @@ PyTypeObject PyEncoderType = { }; static PyMethodDef speedups_methods[] = { - {"encode_basestring_ascii", - (PyCFunction)py_encode_basestring_ascii, - METH_O, - pydoc_encode_basestring_ascii}, - {"encode_basestring", - (PyCFunction)py_encode_basestring, - METH_O, - pydoc_encode_basestring}, - {"scanstring", - (PyCFunction)py_scanstring, - METH_VARARGS, - pydoc_scanstring}, + _JSON_ENCODE_BASESTRING_ASCII_METHODDEF + _JSON_ENCODE_BASESTRING_METHODDEF + _JSON_SCANSTRING_METHODDEF {NULL, NULL, 0, NULL} }; diff -r 395e190ead36 Modules/clinic/_json.c.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Modules/clinic/_json.c.h Tue May 05 09:02:26 2015 +0300 @@ -0,0 +1,60 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +PyDoc_STRVAR(_json_scanstring__doc__, +"scanstring($module, string, end, strict=True, /)\n" +"--\n" +"\n" +"Scan the string for a JSON string.\n" +"\n" +"End is the index of the character in string after the quote that started\n" +"the JSON string. Unescapes all valid JSON string escape sequences and\n" +"raises ValueError on attempt to decode an invalid string. If strict is\n" +"False then literal control characters are allowed in the string.\n" +"\n" +"Returns a tuple of the decoded string and the index of the character in\n" +"string after the end quote."); + +#define _JSON_SCANSTRING_METHODDEF \ + {"scanstring", (PyCFunction)_json_scanstring, METH_VARARGS, _json_scanstring__doc__}, + +static PyObject * +_json_scanstring_impl(PyModuleDef *module, PyObject *string, Py_ssize_t end, + int strict); + +static PyObject * +_json_scanstring(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + PyObject *string; + Py_ssize_t end; + int strict = 1; + + if (!PyArg_ParseTuple(args, "On|i:scanstring", + &string, &end, &strict)) + goto exit; + return_value = _json_scanstring_impl(module, string, end, strict); + +exit: + return return_value; +} + +PyDoc_STRVAR(_json_encode_basestring_ascii__doc__, +"encode_basestring_ascii($module, string, /)\n" +"--\n" +"\n" +"Return an ASCII-only JSON representation of a Python string."); + +#define _JSON_ENCODE_BASESTRING_ASCII_METHODDEF \ + {"encode_basestring_ascii", (PyCFunction)_json_encode_basestring_ascii, METH_O, _json_encode_basestring_ascii__doc__}, + +PyDoc_STRVAR(_json_encode_basestring__doc__, +"encode_basestring($module, string, /)\n" +"--\n" +"\n" +"Return a JSON representation of a Python string."); + +#define _JSON_ENCODE_BASESTRING_METHODDEF \ + {"encode_basestring", (PyCFunction)_json_encode_basestring, METH_O, _json_encode_basestring__doc__}, +/*[clinic end generated code: output=5576d1494bb2ee2d input=a9049054013a1b77]*/