8000 bpo-40077: Convert _jsonmodule to use PyType_FromSpec. by corona10 · Pull Request #19177 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-40077: Convert _jsonmodule to use PyType_FromSpec. #19177

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

Merged
merged 6 commits into from
Mar 27, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
bpo-40077: Apply Victor's review part 1
  • Loading branch information
corona10 committed Mar 26, 2020
commit 5db609a52c808b924938a1674d92a87fd060c7a6
41 changes: 17 additions & 24 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx);
static PyObject *
scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static void
scanner_dealloc(PyScannerObject *self);
scanner_dealloc(PyObject *self);
static int
scanner_clear(PyScannerObject *self);
static PyObject *
encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static void
encoder_dealloc(PyEncoderObject *self);
encoder_dealloc(PyObject *self);
static int
encoder_clear(PyEncoderObject *self);
static int
Expand Down Expand Up @@ -634,14 +634,13 @@ py_encode_basestring(PyObject* Py_UNUSED(self), PyObject *pystr)
}

static void
scanner_dealloc(PyScannerObject *self)
scanner_dealloc(PyObject *self)
{
PyTypeObject *tp = Py_TYPE(self);
/* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(self);
scanner_clear(self);
freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
free_func(self);
scanner_clear((PyScannerObject *)self);
tp->tp_free(self);
Py_DECREF(tp);
}

Expand Down Expand Up @@ -1735,14 +1734,13 @@ encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
}

static void
encoder_dealloc(PyEncoderObject *self)
encoder_dealloc(PyObject *self)
{
PyTypeObject *tp = Py_TYPE(self);
/* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(self);
encoder_clear(self);
freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
free_func(self);
encoder_clear((PyEncoderObject *)self);
tp->tp_free(self);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code is just fine, no? I don't see the value of tp. It seems like it comes from a previous change that you reverted.

Suggested change
tp->tp_free(self);
Py_TYPE(self)->tp_free(self);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vstinner
Py_DECREF(tp);is needed if not test is leaked.
This is why I declared tp ;)

Copy link
Member Author
8000

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_json leaked [114, 114, 114] references, sum=342
test_json failed in 35.6 sec

Py_DECREF(tp);
}

Expand Down Expand Up @@ -1821,6 +1819,7 @@ _json_exec(PyObject *module)
get_json_state(module)->PyScannerType = PyScannerType;
Py_INCREF(PyScannerType);
if (PyModule_AddObject(module, "make_scanner", get_json_state(module)->PyScannerType) < 0) {
Py_DECREF((PyObject*)&PyScannerType);
return -1;
}

Expand All @@ -1831,6 +1830,7 @@ _json_exec(PyObject *module)
get_json_state(module)->PyEncoderType = PyEncoderType;
Py_INCREF(PyEncoderType);
if (PyModule_AddObject(module, "make_encoder", get_json_state(module)->PyEncoderType) < 0) {
Py_DECREF((PyObject*)&PyEncoderType);
return -1;
}
return 0;
Expand All @@ -1839,32 +1839,25 @@ _json_exec(PyObject *module)
static int
_jsonmodule_traverse(PyObject *module, visitproc visit, void *arg)
{
_jsonmodulestate *state = (_jsonmodulestate *)PyModule_GetState(module);
if (state) {
Py_VISIT(get_json_state(module)->PyScannerType);
Py_VISIT(get_json_state(module)->PyEncoderType);
}
_jsonmodulestate *state = get_json_state(module);
Py_VISIT(state->PyScannerType);
Py_VISIT(state->PyEncoderType);
return 0;
}

static int
_jsonmodule_clear(PyObject *module)
{
_jsonmodulestate *state = (_jsonmodulestate *)PyModule_GetState(module);
if (state) {
Py_CLEAR(get_json_state(module)->PyScannerType);
Py_CLEAR(get_json_state(module)->PyEncoderType);
}
_jsonmodulestate *state = get_json_state(module);
Py_CLEAR(state->PyScannerType);
Py_CLEAR(state->PyEncoderType);
return 0;
}

static void
_jsonmodule_free(void *module)
{
_jsonmodulestate *state = (_jsonmodulestate *)PyModule_GetState(module);
if (state) {
_jsonmodule_clear((PyObject *)module);
}
_jsonmodule_clear((PyObject *)module);
}

static PyModuleDef_Slot _json_slots[] = {
Expand Down
0