Skip to content

Commit 25bd72d

Browse files
authored
pythongh-137821: Convert _json module to use Argument Clinic (pythongh-140778)
1 parent b85e10f commit 25bd72d

File tree

3 files changed

+131
-43
lines changed

3 files changed

+131
-43
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert ``_json`` module to use Argument Clinic

Modules/_json.c

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
#include <stdbool.h> // bool
2020

21+
#include "clinic/_json.c.h"
22+
23+
/*[clinic input]
24+
module _json
25+
[clinic start generated code]*/
26+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=549fa53592c925b2]*/
2127

2228
typedef struct _PyScannerObject {
2329
PyObject_HEAD
@@ -637,30 +643,31 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
637643
return NULL;
638644
}
639645

640-
PyDoc_STRVAR(pydoc_scanstring,
641-
"scanstring(string, end, strict=True) -> (string, end)\n"
642-
"\n"
643-
"Scan the string s for a JSON string. End is the index of the\n"
644-
"character in s after the quote that started the JSON string.\n"
645-
"Unescapes all valid JSON string escape sequences and raises ValueError\n"
646-
"on attempt to decode an invalid string. If strict is False then literal\n"
647-
"control characters are allowed in the string.\n"
648-
"\n"
649-
"Returns a tuple of the decoded string and the index of the character in s\n"
650-
"after the end quote."
651-
);
646+
/*[clinic input]
647+
_json.scanstring as py_scanstring
648+
pystr: object
649+
end: Py_ssize_t
650+
strict: bool = True
651+
/
652+
653+
Scan the string s for a JSON string.
654+
655+
End is the index of the character in s after the quote that started the
656+
JSON string. Unescapes all valid JSON string escape sequences and raises
657+
ValueError on attempt to decode an invalid string. If strict is False
658+
then literal control characters are allowed in the string.
659+
660+
Returns a tuple of the decoded string and the index of the character in s
661+
after the end quote.
662+
[clinic start generated code]*/
652663

653664
static PyObject *
654-
py_scanstring(PyObject* Py_UNUSED(self), PyObject *args)
665+
py_scanstring_impl(PyObject *module, PyObject *pystr, Py_ssize_t end,
666+
int strict)
667+
/*[clinic end generated code: output=961740cfae07cdb3 input=9d46d7df7ac749b0]*/
655668
{
656-
PyObject *pystr;
657669
PyObject *rval;
658-
Py_ssize_t end;
659670
Py_ssize_t next_end = -1;
660-
int strict = 1;
661-
if (!PyArg_ParseTuple(args, "On|p:scanstring", &pystr, &end, &strict)) {
662-
return NULL;
663-
}
664671
if (PyUnicode_Check(pystr)) {
665672
rval = scanstring_unicode(pystr, end, strict, &next_end);
666673
}
@@ -673,14 +680,17 @@ py_scanstring(PyObject* Py_UNUSED(self), PyObject *args)
673680
return _build_rval_index_tuple(rval, next_end);
674681
}
675682

676-
PyDoc_STRVAR(pydoc_encode_basestring_ascii,
677-
"encode_basestring_ascii(string) -> string\n"
678-
"\n"
679-
"Return an ASCII-only JSON representation of a Python string"
680-
);
683+
/*[clinic input]
684+
_json.encode_basestring_ascii as py_encode_basestring_ascii
685+
pystr: object
686+
/
687+
688+
Return an ASCII-only JSON representation of a Python string
689+
[clinic start generated code]*/
681690

682691
static PyObject *
683-
py_encode_basestring_ascii(PyObject* Py_UNUSED(self), PyObject *pystr)
692+
py_encode_basestring_ascii(PyObject *module, PyObject *pystr)
693+
/*[clinic end generated code: output=a8afcd88eba0b572 input=f4085ccd5928ea55]*/
684694
{
685695
PyObject *rval;
686696
/* Return an ASCII-only JSON representation of a Python string */
@@ -697,15 +707,17 @@ py_encode_basestring_ascii(PyObject* Py_UNUSED(self), PyObject *pystr)
697707
return rval;
698708
}
699709

710+
/*[clinic input]
711+
_json.encode_basestring as py_encode_basestring
712+
pystr: object
713+
/
700714
701-
PyDoc_STRVAR(pydoc_encode_basestring,
702-
"encode_basestring(string) -> string\n"
703-
"\n"
704-
"Return a JSON representation of a Python string"
705-
);
715+
Return a JSON representation of a Python string
716+
[clinic start generated code]*/
706717

707718
static PyObject *
708-
py_encode_basestring(PyObject* Py_UNUSED(self), PyObject *pystr)
719+
py_encode_basestring(PyObject *module, PyObject *pystr)
720+
/*[clinic end generated code: output=c87752300776d3b1 input=c3c7ef6e72624f6e]*/
709721
{
710722
PyObject *rval;
711723
/* Return a JSON representation of a Python string */
@@ -2080,18 +2092,9 @@ static PyType_Spec PyEncoderType_spec = {
20802092
};
20812093

20822094
static PyMethodDef speedups_methods[] = {
2083-
{"encode_basestring_ascii",
2084-
py_encode_basestring_ascii,
2085-
METH_O,
2086-
pydoc_encode_basestring_ascii},
2087-
{"encode_basestring",
2088-
py_encode_basestring,
2089-
METH_O,
2090-
pydoc_encode_basestring},
2091-
{"scanstring",
2092-
py_scanstring,
2093-
METH_VARARGS,
2094-
pydoc_scanstring},
2095+
PY_ENCODE_BASESTRING_ASCII_METHODDEF
2096+
PY_ENCODE_BASESTRING_METHODDEF
2097+
PY_SCANSTRING_METHODDEF
20952098
{NULL, NULL, 0, NULL}
20962099
};
20972100

Modules/clinic/_json.c.h

Lines changed: 84 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)