Skip to content

Commit 780757a

Browse files
GH-90699: Remove _Py_IDENTIFIER usage from _json module (GH-98956)
1 parent df84b7b commit 780757a

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

Include/internal/pycore_global_strings.h

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct _Py_global_strings {
4444
STRUCT_FOR_STR(dot, ".")
4545
STRUCT_FOR_STR(dot_locals, ".<locals>")
4646
STRUCT_FOR_STR(empty, "")
47+
STRUCT_FOR_STR(json_decoder, "json.decoder")
4748
STRUCT_FOR_STR(list_err, "list index out of range")
4849
STRUCT_FOR_STR(newline, "\n")
4950
STRUCT_FOR_STR(open_br, "{")
@@ -53,6 +54,7 @@ struct _Py_global_strings {
5354

5455
struct {
5556
STRUCT_FOR_ID(False)
57+
STRUCT_FOR_ID(JSONDecodeError)
5658
STRUCT_FOR_ID(Py_Repr)
5759
STRUCT_FOR_ID(TextIOWrapper)
5860
STRUCT_FOR_ID(True)
@@ -352,6 +354,7 @@ struct _Py_global_strings {
352354
STRUCT_FOR_ID(extend)
353355
STRUCT_FOR_ID(facility)
354356
STRUCT_FOR_ID(factory)
357+
STRUCT_FOR_ID(false)
355358
STRUCT_FOR_ID(family)
356359
STRUCT_FOR_ID(fanout)
357360
STRUCT_FOR_ID(fd)
@@ -491,6 +494,7 @@ struct _Py_global_strings {
491494
STRUCT_FOR_ID(node_offset)
492495
STRUCT_FOR_ID(ns)
493496
STRUCT_FOR_ID(nstype)
497+
STRUCT_FOR_ID(null)
494498
STRUCT_FOR_ID(number)
495499
STRUCT_FOR_ID(obj)
496500
STRUCT_FOR_ID(object)
@@ -627,6 +631,7 @@ struct _Py_global_strings {
627631
STRUCT_FOR_ID(traceback)
628632
STRUCT_FOR_ID(trailers)
629633
STRUCT_FOR_ID(translate)
634+
STRUCT_FOR_ID(true)
630635
STRUCT_FOR_ID(truncate)
631636
STRUCT_FOR_ID(twice)
632637
STRUCT_FOR_ID(txt)

Include/internal/pycore_runtime_init_generated.h

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_json.c

+8-29
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
#ifndef Py_BUILD_CORE_BUILTIN
88
# define Py_BUILD_CORE_MODULE 1
99
#endif
10-
#define NEEDS_PY_IDENTIFIER
1110

1211
#include "Python.h"
1312
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
1413
#include "structmember.h" // PyMemberDef
14+
#include "pycore_runtime_init.h" // _Py_ID()
1515
#include <stdbool.h> // bool
1616

1717

@@ -305,15 +305,9 @@ static void
305305
raise_errmsg(const char *msg, PyObject *s, Py_ssize_t end)
306306
{
307307
/* Use JSONDecodeError exception to raise a nice looking ValueError subclass */
308-
_Py_static_string(PyId_decoder, "json.decoder");
309-
PyObject *decoder = _PyImport_GetModuleId(&PyId_decoder);
310-
if (decoder == NULL) {
311-
return;
312-
}
313-
314-
_Py_IDENTIFIER(JSONDecodeError);
315-
PyObject *JSONDecodeError = _PyObject_GetAttrId(decoder, &PyId_JSONDecodeError);
316-
Py_DECREF(decoder);
308+
_Py_DECLARE_STR(json_decoder, "json.decoder");
309+
PyObject *JSONDecodeError =
310+
_PyImport_GetModuleAttr(&_Py_STR(json_decoder), &_Py_ID(JSONDecodeError));
317311
if (JSONDecodeError == NULL) {
318312
return;
319313
}
@@ -1310,28 +1304,13 @@ _encoded_const(PyObject *obj)
13101304
{
13111305
/* Return the JSON string representation of None, True, False */
13121306
if (obj == Py_None) {
1313-
_Py_static_string(PyId_null, "null");
1314-
PyObject *s_null = _PyUnicode_FromId(&PyId_null);
1315-
if (s_null == NULL) {
1316-
return NULL;
1317-
}
1318-
return Py_NewRef(s_null);
1307+
return Py_NewRef(&_Py_ID(null));
13191308
}
13201309
else if (obj == Py_True) {
1321-
_Py_static_string(PyId_true, "true");
1322-
PyObject *s_true = _PyUnicode_FromId(&PyId_true);
1323-
if (s_true == NULL) {
1324-
return NULL;
1325-
}
1326-
return Py_NewRef(s_true);
1310+
return Py_NewRef(&_Py_ID(true));
13271311
}
13281312
else if (obj == Py_False) {
1329-
_Py_static_string(PyId_false, "false");
1330-
PyObject *s_false = _PyUnicode_FromId(&PyId_false);
1331-
if (s_false == NULL) {
1332-
return NULL;
1333-
}
1334-
return Py_NewRef(s_false);
1313+
return Py_NewRef(&_Py_ID(false));
13351314
}
13361315
else {
13371316
PyErr_SetString(PyExc_ValueError, "not a const");
@@ -1530,7 +1509,7 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir
15301509

15311510
if (*first) {
15321511
*first = false;
1533-
}
1512+
}
15341513
else {
15351514
if (_PyUnicodeWriter_WriteStr(writer, s->item_separator) < 0) {
15361515
Py_DECREF(keystr);

0 commit comments

Comments
 (0)