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-99300: Use Py_NewRef() in Modules/ directory #99469

Merged
merged 1 commit into from
Nov 14, 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
21 changes: 7 additions & 14 deletions Modules/cjkcodecs/multibytecodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ codecctx_errors_get(MultibyteStatefulCodecContext *self, void *Py_UNUSED(ignored
else if (self->errors == ERROR_REPLACE)
errors = "replace";
else {
Py_INCREF(self->errors);
return self->errors;
return Py_NewRef(self->errors);
}

return PyUnicode_FromString(errors);
Expand Down Expand Up @@ -341,8 +340,7 @@ multibytecodec_encerror(MultibyteCodec *codec,
goto errorexit;
}
else {
Py_INCREF(tobj);
retstr = tobj;
retstr = Py_NewRef(tobj);
}

assert(PyBytes_Check(retstr));
Expand Down Expand Up @@ -786,11 +784,9 @@ encoder_encode_stateful(MultibyteStatefulEncoderContext *ctx,
if (ctx->pending) {
PyObject *inbuf_tmp;

Py_INCREF(ctx->pending);
origpending = ctx->pending;
origpending = Py_NewRef(ctx->pending);

Py_INCREF(ctx->pending);
inbuf_tmp = ctx->pending;
inbuf_tmp = Py_NewRef(ctx->pending);
PyUnicode_Append(&inbuf_tmp, unistr);
if (inbuf_tmp == NULL)
goto errorexit;
Expand All @@ -800,8 +796,7 @@ encoder_encode_stateful(MultibyteStatefulEncoderContext *ctx,
else {
origpending = NULL;

Py_INCREF(unistr);
inbuf = unistr;
inbuf = Py_NewRef(unistr);
}
if (PyUnicode_READY(inbuf) < 0)
goto errorexit;
Expand Down Expand Up @@ -1645,8 +1640,7 @@ mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}

self->codec = ((MultibyteCodecObject *)codec)->codec;
self->stream = stream;
Py_INCREF(stream);
self->stream = Py_NewRef(stream);
self->pendingsize = 0;
self->errors = internal_error_callback(errors);
if (self->errors == NULL)
Expand Down Expand Up @@ -1869,8 +1863,7 @@ mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}

self->codec = ((MultibyteCodecObject *)codec)->codec;
self->stream = stream;
Py_INCREF(stream);
self->stream = Py_NewRef(stream);
self->pending = NULL;
self->errors = internal_error_callback(errors);
if (self->errors == NULL)
Expand Down
3 changes: 1 addition & 2 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1870,8 +1870,7 @@ gc_is_tracked(PyObject *module, PyObject *obj)
result = Py_True;
else
result = Py_False;
Py_INCREF(result);
return result;
return Py_NewRef(result);
}

/*[clinic input]
Expand Down
29 changes: 10 additions & 19 deletions Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ getpath_isabs(PyObject *Py_UNUSED(self), PyObject *args)
r = _Py_isabs(path) ? Py_True : Py_False;
PyMem_Free((void *)path);
}
Py_XINCREF(r);
return r;
return Py_XNewRef(r);
}


Expand All @@ -153,11 +152,10 @@ getpath_hassuffix(PyObject *Py_UNUSED(self), PyObject *args)
wcscmp(&path[len - suffixLen], suffix) != 0
#endif
) {
r = Py_False;
r = Py_NewRef(Py_False);
} else {
r = Py_True;
r = Py_NewRef(Py_True);
}
Py_INCREF(r);
PyMem_Free((void *)suffix);
}
PyMem_Free((void *)path);
Expand Down Expand Up @@ -187,8 +185,7 @@ getpath_isdir(PyObject *Py_UNUSED(self), PyObject *args)
#endif
PyMem_Free((void *)path);
}
Py_XINCREF(r);
return r;
return Py_XNewRef(r);
}


Expand All @@ -213,8 +210,7 @@ getpath_isfile(PyObject *Py_UNUSED(self), PyObject *args)
#endif
PyMem_Free((void *)path);
}
Py_XINCREF(r);
return r;
return Py_XNewRef(r);
}


Expand Down Expand Up @@ -247,8 +243,7 @@ getpath_isxfile(PyObject *Py_UNUSED(self), PyObject *args)
#endif
PyMem_Free((void *)path);
}
Py_XINCREF(r);
return r;
return Py_XNewRef(r);
}


Expand Down Expand Up @@ -488,8 +483,7 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
goto done;
}
if (!S_ISLNK(st.st_mode)) {
Py_INCREF(pathobj);
r = pathobj;
r = Py_NewRef(pathobj);
goto done;
}
wchar_t resolved[MAXPATHLEN+1];
Expand All @@ -504,8 +498,7 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
return r;
#endif

Py_INCREF(pathobj);
return pathobj;
return Py_NewRef(pathobj);
}


Expand Down Expand Up @@ -591,8 +584,7 @@ wchar_to_dict(PyObject *dict, const char *key, const wchar_t *s)
return 0;
}
} else {
u = Py_None;
Py_INCREF(u);
u = Py_NewRef(Py_None);
}
r = PyDict_SetItemString(dict, key, u) == 0;
Py_DECREF(u);
Expand All @@ -617,8 +609,7 @@ decode_to_dict(PyObject *dict, const char *key, const char *s)
return 0;
}
} else {
u = Py_None;
Py_INCREF(u);
u = Py_NewRef(Py_None);
}
r = PyDict_SetItemString(dict, key, u) == 0;
Py_DECREF(u);
Expand Down
6 changes: 2 additions & 4 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2048,8 +2048,7 @@ factorial_odd_part(unsigned long n)
inner = PyLong_FromLong(1);
if (inner == NULL)
return NULL;
outer = inner;
Py_INCREF(outer);
outer = Py_NewRef(inner);

upper = 3;
for (i = _Py_bit_length(n) - 2; i >= 0; i--) {
Expand Down Expand Up @@ -3521,8 +3520,7 @@ perm_comb(PyObject *n, unsigned long long k, int iscomb)
return PyLong_FromLong(1);
}
if (k == 1) {
Py_INCREF(n);
return n;
return Py_NewRef(n);
}

/* P(n, k) = P(n, j) * P(n-j, k-j) */
Expand Down
9 changes: 3 additions & 6 deletions Modules/ossaudiodev.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,7 @@ oss_close(oss_audio_t *self, PyObject *unused)
static PyObject *
oss_self(PyObject *self, PyObject *unused)
{
Py_INCREF(self);
return self;
return Py_NewRef(self);
}

static PyObject *
Expand Down Expand Up @@ -1135,10 +1134,8 @@ PyInit_ossaudiodev(void)
NULL, NULL);
if (OSSAudioError) {
/* Each call to PyModule_AddObject decrefs it; compensate: */
Py_INCREF(OSSAudioError);
Py_INCREF(OSSAudioError);
PyModule_AddObject(m, "error", OSSAudioError);
PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
PyModule_AddObject(m, "error", Py_NewRef(OSSAudioError));
PyModule_AddObject(m, "OSSAudioError", Py_NewRef(OSSAudioError));
}

/* Build 'control_labels' and 'control_names' lists and add them
Expand Down
12 changes: 4 additions & 8 deletions Modules/overlapped.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,7 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
_PyBytes_Resize(&self->allocated_buffer, transferred))
return NULL;

Py_INCREF(self->allocated_buffer);
return self->allocated_buffer;
return Py_NewRef(self->allocated_buffer);
case TYPE_READ_FROM:
assert(PyBytes_CheckExact(self->read_from.allocated_buffer));

Expand All @@ -940,14 +939,12 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
}

// first item: message
Py_INCREF(self->read_from.allocated_buffer);
PyTuple_SET_ITEM(self->read_from.result, 0,
self->read_from.allocated_buffer);
Py_NewRef(self->read_from.allocated_buffer));
// second item: address
PyTuple_SET_ITEM(self->read_from.result, 1, addr);

Py_INCREF(self->read_from.result);
return self->read_from.result;
return Py_NewRef(self->read_from.result);
case TYPE_READ_FROM_INTO:
// unparse the address
addr = unparse_address((SOCKADDR*)&self->read_from_into.address,
Expand All @@ -970,8 +967,7 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
// second item: address
PyTuple_SET_ITEM(self->read_from_into.result, 1, addr);

Py_INCREF(self->read_from_into.result);
return self->read_from_into.result;
return Py_NewRef(self->read_from_into.result);
default:
return PyLong_FromUnsignedLong((unsigned long) transferred);
}
Expand Down
Loading