Skip to content

Commit c45e6e1

Browse files
authored
gh-137821: Improve Argument Clinic definitions in the _json module (#140780)
1 parent dbe3950 commit c45e6e1

File tree

5 files changed

+59
-47
lines changed

5 files changed

+59
-47
lines changed

Lib/test/test_json/test_scanstring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def test_bad_escapes(self):
144144

145145
def test_overflow(self):
146146
with self.assertRaises(OverflowError):
147-
self.json.decoder.scanstring(b"xxx", sys.maxsize+1)
147+
self.json.decoder.scanstring("xxx", sys.maxsize+1)
148148

149149

150150
class TestPyScanstring(TestScanstring, PyTest): pass

Misc/NEWS.d/next/Core_and_Builtins/2025-10-30-15-33-07.gh-issue-137821.8_Iavt.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Convert ``_json`` module to use Argument Clinic.
2+
Patched by Yoonho Hann.

Modules/_json.c

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
645645

646646
/*[clinic input]
647647
_json.scanstring as py_scanstring
648-
pystr: object
648+
pystr: unicode
649649
end: Py_ssize_t
650650
strict: bool = True
651651
/
@@ -664,74 +664,41 @@ after the end quote.
664664
static PyObject *
665665
py_scanstring_impl(PyObject *module, PyObject *pystr, Py_ssize_t end,
666666
int strict)
667-
/*[clinic end generated code: output=961740cfae07cdb3 input=9d46d7df7ac749b0]*/
667+
/*[clinic end generated code: output=961740cfae07cdb3 input=cff59e47498f4d8e]*/
668668
{
669-
PyObject *rval;
670669
Py_ssize_t next_end = -1;
671-
if (PyUnicode_Check(pystr)) {
672-
rval = scanstring_unicode(pystr, end, strict, &next_end);
673-
}
674-
else {
675-
PyErr_Format(PyExc_TypeError,
676-
"first argument must be a string, not %.80s",
677-
Py_TYPE(pystr)->tp_name);
678-
return NULL;
679-
}
670+
PyObject *rval = scanstring_unicode(pystr, end, strict, &next_end);
680671
return _build_rval_index_tuple(rval, next_end);
681672
}
682673

683674
/*[clinic input]
684675
_json.encode_basestring_ascii as py_encode_basestring_ascii
685-
pystr: object
676+
pystr: unicode
686677
/
687678
688679
Return an ASCII-only JSON representation of a Python string
689680
[clinic start generated code]*/
690681

691682
static PyObject *
692-
py_encode_basestring_ascii(PyObject *module, PyObject *pystr)
693-
/*[clinic end generated code: output=a8afcd88eba0b572 input=f4085ccd5928ea55]*/
683+
py_encode_basestring_ascii_impl(PyObject *module, PyObject *pystr)
684+
/*[clinic end generated code: output=7b3841287cf211df input=4f3609498aff2de5]*/
694685
{
695-
PyObject *rval;
696-
/* Return an ASCII-only JSON representation of a Python string */
697-
/* METH_O */
698-
if (PyUnicode_Check(pystr)) {
699-
rval = ascii_escape_unicode(pystr);
700-
}
701-
else {
702-
PyErr_Format(PyExc_TypeError,
703-
"first argument must be a string, not %.80s",
704-
Py_TYPE(pystr)->tp_name);
705-
return NULL;
706-
}
707-
return rval;
686+
return ascii_escape_unicode(pystr);
708687
}
709688

710689
/*[clinic input]
711690
_json.encode_basestring as py_encode_basestring
712-
pystr: object
691+
pystr: unicode
713692
/
714693
715694
Return a JSON representation of a Python string
716695
[clinic start generated code]*/
717696

718697
static PyObject *
719-
py_encode_basestring(PyObject *module, PyObject *pystr)
720-
/*[clinic end generated code: output=c87752300776d3b1 input=c3c7ef6e72624f6e]*/
698+
py_encode_basestring_impl(PyObject *module, PyObject *pystr)
699+
/*[clinic end generated code: output=900950f95df3f1c9 input=d42ef714b2c07386]*/
721700
{
722-
PyObject *rval;
723-
/* Return a JSON representation of a Python string */
724-
/* METH_O */
725-
if (PyUnicode_Check(pystr)) {
726-
rval = escape_unicode(pystr);
727-
}
728-
else {
729-
PyErr_Format(PyExc_TypeError,
730-
"first argument must be a string, not %.80s",
731-
Py_TYPE(pystr)->tp_name);
732-
return NULL;
733-
}
734-
return rval;
701+
return escape_unicode(pystr);
735702
}
736703

737704
static void

Modules/clinic/_json.c.h

Lines changed: 45 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)