Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-90699: Remove _Py_IDENTIFIER usage from _json module #98956

Merged
merged 6 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct _Py_global_strings {
STRUCT_FOR_STR(dot, ".")
STRUCT_FOR_STR(dot_locals, ".<locals>")
STRUCT_FOR_STR(empty, "")
STRUCT_FOR_STR(json_decoder, "json.decoder")
STRUCT_FOR_STR(list_err, "list index out of range")
STRUCT_FOR_STR(newline, "\n")
STRUCT_FOR_STR(open_br, "{")
Expand All @@ -53,6 +54,7 @@ struct _Py_global_strings {

struct {
STRUCT_FOR_ID(False)
STRUCT_FOR_ID(JSONDecodeError)
STRUCT_FOR_ID(Py_Repr)
STRUCT_FOR_ID(TextIOWrapper)
STRUCT_FOR_ID(True)
Expand Down Expand Up @@ -352,6 +354,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(extend)
STRUCT_FOR_ID(facility)
STRUCT_FOR_ID(factory)
STRUCT_FOR_ID(false)
STRUCT_FOR_ID(family)
STRUCT_FOR_ID(fanout)
STRUCT_FOR_ID(fd)
Expand Down Expand Up @@ -491,6 +494,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(node_offset)
STRUCT_FOR_ID(ns)
STRUCT_FOR_ID(nstype)
STRUCT_FOR_ID(null)
STRUCT_FOR_ID(number)
STRUCT_FOR_ID(obj)
STRUCT_FOR_ID(object)
Expand Down Expand Up @@ -627,6 +631,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(traceback)
STRUCT_FOR_ID(trailers)
STRUCT_FOR_ID(translate)
STRUCT_FOR_ID(true)
STRUCT_FOR_ID(truncate)
STRUCT_FOR_ID(twice)
STRUCT_FOR_ID(txt)
Expand Down
33 changes: 33 additions & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 8 additions & 29 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
#define NEEDS_PY_IDENTIFIER

#include "Python.h"
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
#include "structmember.h" // PyMemberDef
#include "pycore_runtime_init.h" // _Py_ID()
#include <stdbool.h> // bool


Expand Down Expand Up @@ -305,15 +305,9 @@ static void
raise_errmsg(const char *msg, PyObject *s, Py_ssize_t end)
{
/* Use JSONDecodeError exception to raise a nice looking ValueError subclass */
_Py_static_string(PyId_decoder, "json.decoder");
PyObject *decoder = _PyImport_GetModuleId(&PyId_decoder);
if (decoder == NULL) {
return;
}

_Py_IDENTIFIER(JSONDecodeError);
PyObject *JSONDecodeError = _PyObject_GetAttrId(decoder, &PyId_JSONDecodeError);
Py_DECREF(decoder);
_Py_DECLARE_STR(json_decoder, "json.decoder");
PyObject *JSONDecodeError =
_PyImport_GetModuleAttr(&_Py_STR(json_decoder), &_Py_ID(JSONDecodeError));
if (JSONDecodeError == NULL) {
return;
}
Expand Down Expand Up @@ -1310,28 +1304,13 @@ _encoded_const(PyObject *obj)
{
/* Return the JSON string representation of None, True, False */
if (obj == Py_None) {
_Py_static_string(PyId_null, "null");
PyObject *s_null = _PyUnicode_FromId(&PyId_null);
if (s_null == NULL) {
return NULL;
}
return Py_NewRef(s_null);
return Py_NewRef(&_Py_ID(null));
}
else if (obj == Py_True) {
_Py_static_string(PyId_true, "true");
PyObject *s_true = _PyUnicode_FromId(&PyId_true);
if (s_true == NULL) {
return NULL;
}
return Py_NewRef(s_true);
return Py_NewRef(&_Py_ID(true));
}
else if (obj == Py_False) {
_Py_static_string(PyId_false, "false");
PyObject *s_false = _PyUnicode_FromId(&PyId_false);
if (s_false == NULL) {
return NULL;
}
return Py_NewRef(s_false);
return Py_NewRef(&_Py_ID(false));
}
else {
PyErr_SetString(PyExc_ValueError, "not a const");
Expand Down Expand Up @@ -1530,7 +1509,7 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir

if (*first) {
*first = false;
}
}
else {
if (_PyUnicodeWriter_WriteStr(writer, s->item_separator) < 0) {
Py_DECREF(keystr);
Expand Down