gh-99300: Use Py_NewRef() in Modules/ directory (#99468)

Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in test C files of the Modules/ directory.
This commit is contained in:
Victor Stinner 2022-11-14 13:44:56 +01:00 committed by GitHub
parent 9a7e9f9921
commit c340cbb7f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 92 additions and 173 deletions

View File

@ -459,8 +459,7 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
state->start = (void*) ((char*) ptr + start * state->charsize); state->start = (void*) ((char*) ptr + start * state->charsize);
state->end = (void*) ((char*) ptr + end * state->charsize); state->end = (void*) ((char*) ptr + end * state->charsize);
Py_INCREF(string); state->string = Py_NewRef(string);
state->string = string;
state->pos = start; state->pos = start;
state->endpos = end; state->endpos = end;
@ -499,8 +498,7 @@ getslice(int isbytes, const void *ptr,
if (isbytes) { if (isbytes) {
if (PyBytes_CheckExact(string) && if (PyBytes_CheckExact(string) &&
start == 0 && end == PyBytes_GET_SIZE(string)) { start == 0 && end == PyBytes_GET_SIZE(string)) {
Py_INCREF(string); return Py_NewRef(string);
return string;
} }
return PyBytes_FromStringAndSize( return PyBytes_FromStringAndSize(
(const char *)ptr + start, end - start); (const char *)ptr + start, end - start);
@ -1089,8 +1087,7 @@ pattern_subx(_sremodulestate* module_state,
if (PyCallable_Check(ptemplate)) { if (PyCallable_Check(ptemplate)) {
/* sub/subn takes either a function or a template */ /* sub/subn takes either a function or a template */
filter = ptemplate; filter = Py_NewRef(ptemplate);
Py_INCREF(filter);
filter_type = CALLABLE; filter_type = CALLABLE;
} else { } else {
/* if not callable, check if it's a literal string */ /* if not callable, check if it's a literal string */
@ -1109,8 +1106,7 @@ pattern_subx(_sremodulestate* module_state,
if (view.buf) if (view.buf)
PyBuffer_Release(&view); PyBuffer_Release(&view);
if (literal) { if (literal) {
filter = ptemplate; filter = Py_NewRef(ptemplate);
Py_INCREF(filter);
filter_type = LITERAL; filter_type = LITERAL;
} else { } else {
/* not a literal; hand it over to the template compiler */ /* not a literal; hand it over to the template compiler */
@ -1120,8 +1116,8 @@ pattern_subx(_sremodulestate* module_state,
assert(Py_TYPE(filter) == module_state->Template_Type); assert(Py_TYPE(filter) == module_state->Template_Type);
if (Py_SIZE(filter) == 0) { if (Py_SIZE(filter) == 0) {
Py_INCREF(((TemplateObject *)filter)->literal); Py_SETREF(filter,
Py_SETREF(filter, ((TemplateObject *)filter)->literal); Py_NewRef(((TemplateObject *)filter)->literal));
filter_type = LITERAL; filter_type = LITERAL;
} }
else { else {
@ -1195,8 +1191,7 @@ pattern_subx(_sremodulestate* module_state,
goto error; goto error;
} else { } else {
/* filter is literal string */ /* filter is literal string */
item = filter; item = Py_NewRef(filter);
Py_INCREF(item);
} }
/* add to list */ /* add to list */
@ -1317,8 +1312,7 @@ static PyObject *
_sre_SRE_Pattern___copy___impl(PatternObject *self) _sre_SRE_Pattern___copy___impl(PatternObject *self)
/*[clinic end generated code: output=85dedc2db1bd8694 input=a730a59d863bc9f5]*/ /*[clinic end generated code: output=85dedc2db1bd8694 input=a730a59d863bc9f5]*/
{ {
Py_INCREF(self); return Py_NewRef(self);
return (PyObject *)self;
} }
/*[clinic input] /*[clinic input]
@ -1333,8 +1327,7 @@ static PyObject *
_sre_SRE_Pattern___deepcopy__(PatternObject *self, PyObject *memo) _sre_SRE_Pattern___deepcopy__(PatternObject *self, PyObject *memo)
/*[clinic end generated code: output=2ad25679c1f1204a input=a465b1602f997bed]*/ /*[clinic end generated code: output=2ad25679c1f1204a input=a465b1602f997bed]*/
{ {
Py_INCREF(self); return Py_NewRef(self);
return (PyObject *)self;
} }
static PyObject * static PyObject *
@ -1500,19 +1493,16 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
PyBuffer_Release(&view); PyBuffer_Release(&view);
} }
Py_INCREF(pattern); self->pattern = Py_NewRef(pattern);
self->pattern = pattern;
self->flags = flags; self->flags = flags;
self->groups = groups; self->groups = groups;
if (PyDict_GET_SIZE(groupindex) > 0) { if (PyDict_GET_SIZE(groupindex) > 0) {
Py_INCREF(groupindex); self->groupindex = Py_NewRef(groupindex);
self->groupindex = groupindex;
if (PyTuple_GET_SIZE(indexgroup) > 0) { if (PyTuple_GET_SIZE(indexgroup) > 0) {
Py_INCREF(indexgroup); self->indexgroup = Py_NewRef(indexgroup);
self->indexgroup = indexgroup;
} }
} }
@ -1555,8 +1545,7 @@ _sre_template_impl(PyObject *module, PyObject *pattern, PyObject *template)
if (!self) if (!self)
return NULL; return NULL;
self->chunks = 1 + 2*n; self->chunks = 1 + 2*n;
self->literal = PyList_GET_ITEM(template, 0); self->literal = Py_NewRef(PyList_GET_ITEM(template, 0));
Py_INCREF(self->literal);
for (Py_ssize_t i = 0; i < n; i++) { for (Py_ssize_t i = 0; i < n; i++) {
Py_ssize_t index = PyLong_AsSsize_t(PyList_GET_ITEM(template, 2*i+1)); Py_ssize_t index = PyLong_AsSsize_t(PyList_GET_ITEM(template, 2*i+1));
if (index == -1 && PyErr_Occurred()) { if (index == -1 && PyErr_Occurred()) {
@ -1576,8 +1565,7 @@ _sre_template_impl(PyObject *module, PyObject *pattern, PyObject *template)
literal = NULL; literal = NULL;
self->chunks--; self->chunks--;
} }
Py_XINCREF(literal); self->items[i].literal = Py_XNewRef(literal);
self->items[i].literal = literal;
} }
return (PyObject*) self; return (PyObject*) self;
@ -2128,8 +2116,7 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
if (self->string == Py_None || self->mark[index] < 0) { if (self->string == Py_None || self->mark[index] < 0) {
/* return default value if the string or group is undefined */ /* return default value if the string or group is undefined */
Py_INCREF(def); return Py_NewRef(def);
return def;
} }
ptr = getstring(self->string, &length, &isbytes, &charsize, &view); ptr = getstring(self->string, &length, &isbytes, &charsize, &view);
@ -2448,8 +2435,7 @@ match_regs(MatchObject* self)
PyTuple_SET_ITEM(regs, index, item); PyTuple_SET_ITEM(regs, index, item);
} }
Py_INCREF(regs); self->regs = Py_NewRef(regs);
self->regs = regs;
return regs; return regs;
} }
@ -2463,8 +2449,7 @@ static PyObject *
_sre_SRE_Match___copy___impl(MatchObject *self) _sre_SRE_Match___copy___impl(MatchObject *self)
/*[clinic end generated code: output=a779c5fc8b5b4eb4 input=3bb4d30b6baddb5b]*/ /*[clinic end generated code: output=a779c5fc8b5b4eb4 input=3bb4d30b6baddb5b]*/
{ {
Py_INCREF(self); return Py_NewRef(self);
return (PyObject *)self;
} }
/*[clinic input] /*[clinic input]
@ -2479,8 +2464,7 @@ static PyObject *
_sre_SRE_Match___deepcopy__(MatchObject *self, PyObject *memo) _sre_SRE_Match___deepcopy__(MatchObject *self, PyObject *memo)
/*[clinic end generated code: output=ba7cb46d655e4ee2 input=779d12a31c2c325e]*/ /*[clinic end generated code: output=ba7cb46d655e4ee2 input=779d12a31c2c325e]*/
{ {
Py_INCREF(self); return Py_NewRef(self);
return (PyObject *)self;
} }
PyDoc_STRVAR(match_doc, PyDoc_STRVAR(match_doc,
@ -2509,8 +2493,7 @@ match_lastgroup_get(MatchObject *self, void *Py_UNUSED(ignored))
{ {
PyObject *result = PyTuple_GET_ITEM(self->pattern->indexgroup, PyObject *result = PyTuple_GET_ITEM(self->pattern->indexgroup,
self->lastindex); self->lastindex);
Py_INCREF(result); return Py_NewRef(result);
return result;
} }
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -2519,8 +2502,7 @@ static PyObject *
match_regs_get(MatchObject *self, void *Py_UNUSED(ignored)) match_regs_get(MatchObject *self, void *Py_UNUSED(ignored))
{ {
if (self->regs) { if (self->regs) {
Py_INCREF(self->regs); return Py_NewRef(self->regs);
return self->regs;
} else } else
return match_regs(self); return match_regs(self);
} }
@ -2564,11 +2546,9 @@ pattern_new_match(_sremodulestate* module_state,
if (!match) if (!match)
return NULL; return NULL;
Py_INCREF(pattern); match->pattern = (PatternObject*)Py_NewRef(pattern);
match->pattern = pattern;
Py_INCREF(state->string); match->string = Py_NewRef(state->string);
match->string = state->string;
match->regs = NULL; match->regs = NULL;
match->groups = pattern->groups+1; match->groups = pattern->groups+1;
@ -2788,8 +2768,7 @@ pattern_scanner(_sremodulestate *module_state,
return NULL; return NULL;
} }
Py_INCREF(self); scanner->pattern = Py_NewRef(self);
scanner->pattern = (PyObject*) self;
PyObject_GC_Track(scanner); PyObject_GC_Track(scanner);
return (PyObject*) scanner; return (PyObject*) scanner;
@ -2834,8 +2813,7 @@ static PyObject *
expand_template(TemplateObject *self, MatchObject *match) expand_template(TemplateObject *self, MatchObject *match)
{ {
if (Py_SIZE(self) == 0) { if (Py_SIZE(self) == 0) {
Py_INCREF(self->literal); return Py_NewRef(self->literal);
return self->literal;
} }
PyObject *result = NULL; PyObject *result = NULL;
@ -2855,8 +2833,7 @@ expand_template(TemplateObject *self, MatchObject *match)
out = &PyList_GET_ITEM(list, 0); out = &PyList_GET_ITEM(list, 0);
} }
Py_INCREF(self->literal); out[count++] = Py_NewRef(self->literal);
out[count++] = self->literal;
for (Py_ssize_t i = 0; i < Py_SIZE(self); i++) { for (Py_ssize_t i = 0; i < Py_SIZE(self); i++) {
Py_ssize_t index = self->items[i].index; Py_ssize_t index = self->items[i].index;
if (index >= match->groups) { if (index >= match->groups) {
@ -2868,15 +2845,13 @@ expand_template(TemplateObject *self, MatchObject *match)
goto cleanup; goto cleanup;
} }
if (item != Py_None) { if (item != Py_None) {
Py_INCREF(item); out[count++] = Py_NewRef(item);
out[count++] = item;
} }
Py_DECREF(item); Py_DECREF(item);
PyObject *literal = self->items[i].literal; PyObject *literal = self->items[i].literal;
if (literal != NULL) { if (literal != NULL) {
Py_INCREF(literal); out[count++] = Py_NewRef(literal);
out[count++] = literal;
} }
} }

View File

@ -415,8 +415,7 @@ static PyObject *
SSLError_str(PyOSErrorObject *self) SSLError_str(PyOSErrorObject *self)
{ {
if (self->strerror != NULL && PyUnicode_Check(self->strerror)) { if (self->strerror != NULL && PyUnicode_Check(self->strerror)) {
Py_INCREF(self->strerror); return Py_NewRef(self->strerror);
return self->strerror;
} }
else else
return PyObject_Str(self->args); return PyObject_Str(self->args);
@ -500,8 +499,7 @@ fill_and_set_sslerror(_sslmodulestate *state,
if (verify_str != NULL) { if (verify_str != NULL) {
verify_obj = PyUnicode_FromString(verify_str); verify_obj = PyUnicode_FromString(verify_str);
} else { } else {
verify_obj = Py_None; verify_obj = Py_NewRef(Py_None);
Py_INCREF(verify_obj);
} }
break; break;
} }
@ -800,8 +798,7 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
self->ssl = NULL; self->ssl = NULL;
self->Socket = NULL; self->Socket = NULL;
self->ctx = sslctx; self->ctx = (PySSLContext*)Py_NewRef(sslctx);
Py_INCREF(sslctx);
self->shutdown_seen_zero = 0; self->shutdown_seen_zero = 0;
self->owner = NULL; self->owner = NULL;
self->server_hostname = NULL; self->server_hostname = NULL;
@ -1026,8 +1023,7 @@ _asn1obj2py(_sslmodulestate *state, const ASN1_OBJECT *name, int no_name)
} }
} }
if (!buflen && no_name) { if (!buflen && no_name) {
Py_INCREF(Py_None); name_obj = Py_NewRef(Py_None);
name_obj = Py_None;
} }
else { else {
name_obj = PyUnicode_FromStringAndSize(namebuf, buflen); name_obj = PyUnicode_FromStringAndSize(namebuf, buflen);
@ -1876,8 +1872,7 @@ _ssl__SSLSocket_get_unverified_chain_impl(PySSLSocket *self)
X509 *peer = SSL_get_peer_certificate(self->ssl); X509 *peer = SSL_get_peer_certificate(self->ssl);
if (peer == NULL) { if (peer == NULL) {
peerobj = Py_None; peerobj = Py_NewRef(Py_None);
Py_INCREF(peerobj);
} else { } else {
/* consume X509 reference on success */ /* consume X509 reference on success */
peerobj = _PySSL_CertificateFromX509(self->ctx->state, peer, 0); peerobj = _PySSL_CertificateFromX509(self->ctx->state, peer, 0);
@ -1907,8 +1902,7 @@ cipher_to_tuple(const SSL_CIPHER *cipher)
cipher_name = SSL_CIPHER_get_name(cipher); cipher_name = SSL_CIPHER_get_name(cipher);
if (cipher_name == NULL) { if (cipher_name == NULL) {
Py_INCREF(Py_None); PyTuple_SET_ITEM(retval, 0, Py_NewRef(Py_None));
PyTuple_SET_ITEM(retval, 0, Py_None);
} else { } else {
v = PyUnicode_FromString(cipher_name); v = PyUnicode_FromString(cipher_name);
if (v == NULL) if (v == NULL)
@ -1918,8 +1912,7 @@ cipher_to_tuple(const SSL_CIPHER *cipher)
cipher_protocol = SSL_CIPHER_get_version(cipher); cipher_protocol = SSL_CIPHER_get_version(cipher);
if (cipher_protocol == NULL) { if (cipher_protocol == NULL) {
Py_INCREF(Py_None); PyTuple_SET_ITEM(retval, 1, Py_NewRef(Py_None));
PyTuple_SET_ITEM(retval, 1, Py_None);
} else { } else {
v = PyUnicode_FromString(cipher_protocol); v = PyUnicode_FromString(cipher_protocol);
if (v == NULL) if (v == NULL)
@ -2103,16 +2096,14 @@ _ssl__SSLSocket_compression_impl(PySSLSocket *self)
} }
static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) { static PySSLContext *PySSL_get_context(PySSLSocket *self, void *closure) {
Py_INCREF(self->ctx); return (PySSLContext*)Py_NewRef(self->ctx);
return self->ctx;
} }
static int PySSL_set_context(PySSLSocket *self, PyObject *value, static int PySSL_set_context(PySSLSocket *self, PyObject *value,
void *closure) { void *closure) {
if (PyObject_TypeCheck(value, self->ctx->state->PySSLContext_Type)) { if (PyObject_TypeCheck(value, self->ctx->state->PySSLContext_Type)) {
Py_INCREF(value); Py_SETREF(self->ctx, (PySSLContext *)Py_NewRef(value));
Py_SETREF(self->ctx, (PySSLContext *)value);
SSL_set_SSL_CTX(self->ssl, self->ctx->ctx); SSL_set_SSL_CTX(self->ssl, self->ctx->ctx);
/* Set SSL* internal msg_callback to state of new context's state */ /* Set SSL* internal msg_callback to state of new context's state */
SSL_set_msg_callback( SSL_set_msg_callback(
@ -2150,8 +2141,7 @@ PySSL_get_server_hostname(PySSLSocket *self, void *c)
{ {
if (self->server_hostname == NULL) if (self->server_hostname == NULL)
Py_RETURN_NONE; Py_RETURN_NONE;
Py_INCREF(self->server_hostname); return Py_NewRef(self->server_hostname);
return self->server_hostname;
} }
PyDoc_STRVAR(PySSL_get_server_hostname_doc, PyDoc_STRVAR(PySSL_get_server_hostname_doc,
@ -2166,8 +2156,7 @@ PySSL_get_owner(PySSLSocket *self, void *c)
Py_RETURN_NONE; Py_RETURN_NONE;
owner = PyWeakref_GetObject(self->owner); owner = PyWeakref_GetObject(self->owner);
Py_INCREF(owner); return Py_NewRef(owner);
return owner;
} }
static int static int
@ -2820,8 +2809,7 @@ PySSL_get_session(PySSLSocket *self, void *closure) {
} }
assert(self->ctx); assert(self->ctx);
pysess->ctx = self->ctx; pysess->ctx = (PySSLContext*)Py_NewRef(self->ctx);
Py_INCREF(pysess->ctx);
pysess->session = session; pysess->session = session;
PyObject_GC_Track(pysess); PyObject_GC_Track(pysess);
return (PyObject *)pysess; return (PyObject *)pysess;
@ -4459,8 +4447,7 @@ get_sni_callback(PySSLContext *self, void *c)
if (cb == NULL) { if (cb == NULL) {
Py_RETURN_NONE; Py_RETURN_NONE;
} }
Py_INCREF(cb); return Py_NewRef(cb);
return cb;
} }
static int static int
@ -4482,8 +4469,7 @@ set_sni_callback(PySSLContext *self, PyObject *arg, void *c)
"not a callable object"); "not a callable object");
return -1; return -1;
} }
Py_INCREF(arg); self->set_sni_cb = Py_NewRef(arg);
self->set_sni_cb = arg;
SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback); SSL_CTX_set_tlsext_servername_callback(self->ctx, _servername_callback);
SSL_CTX_set_tlsext_servername_arg(self->ctx, self); SSL_CTX_set_tlsext_servername_arg(self->ctx, self);
} }
@ -5196,7 +5182,7 @@ _ssl_get_default_verify_paths_impl(PyObject *module)
#define CONVERT(info, target) { \ #define CONVERT(info, target) { \
const char *tmp = (info); \ const char *tmp = (info); \
target = NULL; \ target = NULL; \
if (!tmp) { Py_INCREF(Py_None); target = Py_None; } \ if (!tmp) { target = Py_NewRef(Py_None); } \
else if ((target = PyUnicode_DecodeFSDefault(tmp)) == NULL) { \ else if ((target = PyUnicode_DecodeFSDefault(tmp)) == NULL) { \
target = PyBytes_FromString(tmp); } \ target = PyBytes_FromString(tmp); } \
if (!target) goto error; \ if (!target) goto error; \
@ -5311,11 +5297,9 @@ certEncodingType(DWORD encodingType)
} }
switch(encodingType) { switch(encodingType) {
case X509_ASN_ENCODING: case X509_ASN_ENCODING:
Py_INCREF(x509_asn); return Py_NewRef(x509_asn);
return x509_asn;
case PKCS_7_ASN_ENCODING: case PKCS_7_ASN_ENCODING:
Py_INCREF(pkcs_7_asn); return Py_NewRef(pkcs_7_asn);
return pkcs_7_asn;
default: default:
return PyLong_FromLong(encodingType); return PyLong_FromLong(encodingType);
} }
@ -5717,8 +5701,7 @@ sslmodule_init_socketapi(PyObject *module)
if ((sockmod == NULL) || (sockmod->Sock_Type == NULL)) { if ((sockmod == NULL) || (sockmod->Sock_Type == NULL)) {
return -1; return -1;
} }
state->Sock_Type = sockmod->Sock_Type; state->Sock_Type = (PyTypeObject*)Py_NewRef(sockmod->Sock_Type);
Py_INCREF(state->Sock_Type);
return 0; return 0;
} }
@ -5925,8 +5908,7 @@ sslmodule_init_constants(PyObject *m)
#define addbool(m, key, value) \ #define addbool(m, key, value) \
do { \ do { \
PyObject *bool_obj = (value) ? Py_True : Py_False; \ PyObject *bool_obj = (value) ? Py_True : Py_False; \
Py_INCREF(bool_obj); \ PyModule_AddObject((m), (key), Py_NewRef(bool_obj)); \
PyModule_AddObject((m), (key), bool_obj); \
} while (0) } while (0)
addbool(m, "HAS_SNI", 1); addbool(m, "HAS_SNI", 1);

View File

@ -87,8 +87,7 @@ _PySSL_msg_callback(int write_p, int version, int content_type,
static PyObject * static PyObject *
_PySSLContext_get_msg_callback(PySSLContext *self, void *c) { _PySSLContext_get_msg_callback(PySSLContext *self, void *c) {
if (self->msg_cb != NULL) { if (self->msg_cb != NULL) {
Py_INCREF(self->msg_cb); return Py_NewRef(self->msg_cb);
return self->msg_cb;
} else { } else {
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -107,8 +106,7 @@ _PySSLContext_set_msg_callback(PySSLContext *self, PyObject *arg, void *c) {
"not a callable object"); "not a callable object");
return -1; return -1;
} }
Py_INCREF(arg); self->msg_cb = Py_NewRef(arg);
self->msg_cb = arg;
SSL_CTX_set_msg_callback(self->ctx, _PySSL_msg_callback); SSL_CTX_set_msg_callback(self->ctx, _PySSL_msg_callback);
} }
return 0; return 0;
@ -166,8 +164,7 @@ _PySSL_keylog_callback(const SSL *ssl, const char *line)
static PyObject * static PyObject *
_PySSLContext_get_keylog_filename(PySSLContext *self, void *c) { _PySSLContext_get_keylog_filename(PySSLContext *self, void *c) {
if (self->keylog_filename != NULL) { if (self->keylog_filename != NULL) {
Py_INCREF(self->keylog_filename); return Py_NewRef(self->keylog_filename);
return self->keylog_filename;
} else { } else {
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -203,8 +200,7 @@ _PySSLContext_set_keylog_filename(PySSLContext *self, PyObject *arg, void *c) {
"Can't malloc memory for keylog file"); "Can't malloc memory for keylog file");
return -1; return -1;
} }
Py_INCREF(arg); self->keylog_filename = Py_NewRef(arg);
self->keylog_filename = arg;
/* Write a header for seekable, empty files (this excludes pipes). */ /* Write a header for seekable, empty files (this excludes pipes). */
PySSL_BEGIN_ALLOW_THREADS PySSL_BEGIN_ALLOW_THREADS

View File

@ -1829,8 +1829,7 @@ Struct_iter_unpack(PyStructObject *self, PyObject *buffer)
Py_DECREF(iter); Py_DECREF(iter);
return NULL; return NULL;
} }
Py_INCREF(self); iter->so = (PyStructObject*)Py_NewRef(self);
iter->so = self;
iter->index = 0; iter->index = 0;
return (PyObject *)iter; return (PyObject *)iter;
} }
@ -2178,8 +2177,7 @@ cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
s_object = PyDict_GetItemWithError(state->cache, fmt); s_object = PyDict_GetItemWithError(state->cache, fmt);
if (s_object != NULL) { if (s_object != NULL) {
Py_INCREF(s_object); *ptr = (PyStructObject *)Py_NewRef(s_object);
*ptr = (PyStructObject *)s_object;
return Py_CLEANUP_SUPPORTED; return Py_CLEANUP_SUPPORTED;
} }
else if (PyErr_Occurred()) { else if (PyErr_Occurred()) {

View File

@ -784,16 +784,14 @@ PyTclObject_string(PyTclObject *self, void *ignored)
if (!self->string) if (!self->string)
return NULL; return NULL;
} }
Py_INCREF(self->string); return Py_NewRef(self->string);
return self->string;
} }
static PyObject * static PyObject *
PyTclObject_str(PyTclObject *self) PyTclObject_str(PyTclObject *self)
{ {
if (self->string) { if (self->string) {
Py_INCREF(self->string); return Py_NewRef(self->string);
return self->string;
} }
/* XXX Could cache result if it is non-ASCII. */ /* XXX Could cache result if it is non-ASCII. */
return unicodeFromTclObj(self->value); return unicodeFromTclObj(self->value);
@ -1736,8 +1734,7 @@ SetVar(TkappObject *self, PyObject *args, int flags)
if (!ok) if (!ok)
Tkinter_Error(self); Tkinter_Error(self);
else { else {
res = Py_None; res = Py_NewRef(Py_None);
Py_INCREF(res);
} }
LEAVE_OVERLAP_TCL LEAVE_OVERLAP_TCL
break; break;
@ -1755,8 +1752,7 @@ SetVar(TkappObject *self, PyObject *args, int flags)
if (!ok) if (!ok)
Tkinter_Error(self); Tkinter_Error(self);
else { else {
res = Py_None; res = Py_NewRef(Py_None);
Py_INCREF(res);
} }
LEAVE_OVERLAP_TCL LEAVE_OVERLAP_TCL
break; break;
@ -1842,8 +1838,7 @@ UnsetVar(TkappObject *self, PyObject *args, int flags)
if (code == TCL_ERROR) if (code == TCL_ERROR)
res = Tkinter_Error(self); res = Tkinter_Error(self);
else { else {
Py_INCREF(Py_None); res = Py_NewRef(Py_None);
res = Py_None;
} }
LEAVE_OVERLAP_TCL LEAVE_OVERLAP_TCL
return res; return res;
@ -1883,8 +1878,7 @@ _tkinter_tkapp_getint(TkappObject *self, PyObject *arg)
PyObject *result; PyObject *result;
if (PyLong_Check(arg)) { if (PyLong_Check(arg)) {
Py_INCREF(arg); return Py_NewRef(arg);
return arg;
} }
if (PyTclObject_Check(arg)) { if (PyTclObject_Check(arg)) {
@ -1928,8 +1922,7 @@ _tkinter_tkapp_getdouble(TkappObject *self, PyObject *arg)
double v; double v;
if (PyFloat_Check(arg)) { if (PyFloat_Check(arg)) {
Py_INCREF(arg); return Py_NewRef(arg);
return arg;
} }
if (PyNumber_Check(arg)) { if (PyNumber_Check(arg)) {
@ -2145,8 +2138,7 @@ _tkinter_tkapp_splitlist(TkappObject *self, PyObject *arg)
return v; return v;
} }
if (PyTuple_Check(arg)) { if (PyTuple_Check(arg)) {
Py_INCREF(arg); return Py_NewRef(arg);
return arg;
} }
if (PyList_Check(arg)) { if (PyList_Check(arg)) {
return PySequence_Tuple(arg); return PySequence_Tuple(arg);
@ -2322,10 +2314,8 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
data = PyMem_NEW(PythonCmd_ClientData, 1); data = PyMem_NEW(PythonCmd_ClientData, 1);
if (!data) if (!data)
return PyErr_NoMemory(); return PyErr_NoMemory();
Py_INCREF(self); data->self = Py_NewRef(self);
Py_INCREF(func); data->func = Py_NewRef(func);
data->self = (PyObject *) self;
data->func = func;
if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
Tcl_Condition cond = NULL; Tcl_Condition cond = NULL;
CommandEvent *ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent)); CommandEvent *ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent));
@ -2430,10 +2420,8 @@ NewFHCD(PyObject *func, PyObject *file, int id)
FileHandler_ClientData *p; FileHandler_ClientData *p;
p = PyMem_NEW(FileHandler_ClientData, 1); p = PyMem_NEW(FileHandler_ClientData, 1);
if (p != NULL) { if (p != NULL) {
Py_XINCREF(func); p->func = Py_XNewRef(func);
Py_XINCREF(file); p->file = Py_XNewRef(file);
p->func = func;
p->file = file;
p->id = id; p->id = id;
p->next = HeadFHCD; p->next = HeadFHCD;
HeadFHCD = p; HeadFHCD = p;
@ -2591,13 +2579,11 @@ Tktt_New(PyObject *func)
if (v == NULL) if (v == NULL)
return NULL; return NULL;
Py_INCREF(func);
v->token = NULL; v->token = NULL;
v->func = func; v->func = Py_NewRef(func);
/* Extra reference, deleted when called or when handler is deleted */ /* Extra reference, deleted when called or when handler is deleted */
Py_INCREF(v); return (TkttObject*)Py_NewRef(v);
return v;
} }
static void static void
@ -2940,9 +2926,8 @@ _flatten1(FlattenContext* context, PyObject* item, int depth)
if (context->size + 1 > context->maxsize && if (context->size + 1 > context->maxsize &&
!_bump(context, 1)) !_bump(context, 1))
return 0; return 0;
Py_INCREF(o);
PyTuple_SET_ITEM(context->tuple, PyTuple_SET_ITEM(context->tuple,
context->size++, o); context->size++, Py_NewRef(o));
} }
} }
} else { } else {
@ -3266,8 +3251,7 @@ PyInit__tkinter(void)
Py_DECREF(m); Py_DECREF(m);
return NULL; return NULL;
} }
Py_INCREF(o); if (PyModule_AddObject(m, "TclError", Py_NewRef(o))) {
if (PyModule_AddObject(m, "TclError", o)) {
Py_DECREF(o); Py_DECREF(o);
Py_DECREF(m); Py_DECREF(m);
return NULL; return NULL;

View File

@ -347,8 +347,8 @@ tracemalloc_get_frame(_PyInterpreterFrame *pyframe, frame_t *frame)
else { else {
/* tracemalloc_filenames is responsible to keep a reference /* tracemalloc_filenames is responsible to keep a reference
to the filename */ to the filename */
Py_INCREF(filename); if (_Py_hashtable_set(tracemalloc_filenames, Py_NewRef(filename),
if (_Py_hashtable_set(tracemalloc_filenames, filename, NULL) < 0) { NULL) < 0) {
Py_DECREF(filename); Py_DECREF(filename);
#ifdef TRACE_DEBUG #ifdef TRACE_DEBUG
tracemalloc_error("failed to intern the filename"); tracemalloc_error("failed to intern the filename");
@ -1085,8 +1085,7 @@ frame_to_pyobject(frame_t *frame)
if (frame_obj == NULL) if (frame_obj == NULL)
return NULL; return NULL;
Py_INCREF(frame->filename); PyTuple_SET_ITEM(frame_obj, 0, Py_NewRef(frame->filename));
PyTuple_SET_ITEM(frame_obj, 0, frame->filename);
lineno_obj = PyLong_FromUnsignedLong(frame->lineno); lineno_obj = PyLong_FromUnsignedLong(frame->lineno);
if (lineno_obj == NULL) { if (lineno_obj == NULL) {
@ -1107,8 +1106,7 @@ traceback_to_pyobject(traceback_t *traceback, _Py_hashtable_t *intern_table)
if (intern_table != NULL) { if (intern_table != NULL) {
frames = _Py_hashtable_get(intern_table, (const void *)traceback); frames = _Py_hashtable_get(intern_table, (const void *)traceback);
if (frames) { if (frames) {
Py_INCREF(frames); return Py_NewRef(frames);
return frames;
} }
} }

View File

@ -23,8 +23,7 @@ static PyObject *
_typing__idfunc(PyObject *module, PyObject *x) _typing__idfunc(PyObject *module, PyObject *x)
/*[clinic end generated code: output=63c38be4a6ec5f2c input=49f17284b43de451]*/ /*[clinic end generated code: output=63c38be4a6ec5f2c input=49f17284b43de451]*/
{ {
Py_INCREF(x); return Py_NewRef(x);
return x;
} }

View File

@ -291,8 +291,7 @@ _winapi_Overlapped_getbuffer_impl(OverlappedObject *self)
return NULL; return NULL;
} }
res = self->read_buffer ? self->read_buffer : Py_None; res = self->read_buffer ? self->read_buffer : Py_None;
Py_INCREF(res); return Py_NewRef(res);
return res;
} }
/*[clinic input] /*[clinic input]

View File

@ -1722,8 +1722,7 @@ _channelid_shared(PyObject *obj, _PyCrossInterpreterData *data)
xid->resolve = ((channelid *)obj)->resolve; xid->resolve = ((channelid *)obj)->resolve;
data->data = xid; data->data = xid;
Py_INCREF(obj); data->obj = Py_NewRef(obj);
data->obj = obj;
data->new_object = _channelid_from_xid; data->new_object = _channelid_from_xid;
data->free = PyMem_Free; data->free = PyMem_Free;
return 0; return 0;
@ -2634,12 +2633,12 @@ PyInit__xxsubinterpreters(void)
} }
/* Add other types */ /* Add other types */
Py_INCREF(&ChannelIDtype); if (PyDict_SetItemString(ns, "ChannelID",
if (PyDict_SetItemString(ns, "ChannelID", (PyObject *)&ChannelIDtype) != 0) { Py_NewRef(&ChannelIDtype)) != 0) {
return NULL; return NULL;
} }
Py_INCREF(&_PyInterpreterID_Type); if (PyDict_SetItemString(ns, "InterpreterID",
if (PyDict_SetItemString(ns, "InterpreterID", (PyObject *)&_PyInterpreterID_Type) != 0) { Py_NewRef(&_PyInterpreterID_Type)) != 0) {
return NULL; return NULL;
} }

View File

@ -709,8 +709,7 @@ array_richcompare(PyObject *v, PyObject *w, int op)
res = Py_False; res = Py_False;
else else
res = Py_True; res = Py_True;
Py_INCREF(res); return Py_NewRef(res);
return res;
} }
if (va->ob_descr == wa->ob_descr && va->ob_descr->compareitems != NULL) { if (va->ob_descr == wa->ob_descr && va->ob_descr->compareitems != NULL) {
@ -733,8 +732,7 @@ array_richcompare(PyObject *v, PyObject *w, int op)
default: return NULL; /* cannot happen */ default: return NULL; /* cannot happen */
} }
PyObject *res = cmp ? Py_True : Py_False; PyObject *res = cmp ? Py_True : Py_False;
Py_INCREF(res); return Py_NewRef(res);
return res;
} }
@ -778,18 +776,15 @@ array_richcompare(PyObject *v, PyObject *w, int op)
res = Py_True; res = Py_True;
else else
res = Py_False; res = Py_False;
Py_INCREF(res); return Py_NewRef(res);
return res;
} }
/* We have an item that differs. First, shortcuts for EQ/NE */ /* We have an item that differs. First, shortcuts for EQ/NE */
if (op == Py_EQ) { if (op == Py_EQ) {
Py_INCREF(Py_False); res = Py_NewRef(Py_False);
res = Py_False;
} }
else if (op == Py_NE) { else if (op == Py_NE) {
Py_INCREF(Py_True); res = Py_NewRef(Py_True);
res = Py_True;
} }
else { else {
/* Compare the final item again using the proper operator */ /* Compare the final item again using the proper operator */
@ -1060,8 +1055,7 @@ array_inplace_concat(arrayobject *self, PyObject *bb)
} }
if (array_do_extend(state, self, bb) == -1) if (array_do_extend(state, self, bb) == -1)
return NULL; return NULL;
Py_INCREF(self); return Py_NewRef(self);
return (PyObject *)self;
} }
static PyObject * static PyObject *
@ -1085,8 +1079,7 @@ array_inplace_repeat(arrayobject *self, Py_ssize_t n)
_PyBytes_Repeat(self->ob_item, n*size, self->ob_item, size); _PyBytes_Repeat(self->ob_item, n*size, self->ob_item, size);
} }
Py_INCREF(self); return Py_NewRef(self);
return (PyObject *)self;
} }
@ -1947,9 +1940,8 @@ make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
Py_DECREF(typecode_obj); Py_DECREF(typecode_obj);
return NULL; return NULL;
} }
Py_INCREF(items);
PyTuple_SET_ITEM(new_args, 0, typecode_obj); PyTuple_SET_ITEM(new_args, 0, typecode_obj);
PyTuple_SET_ITEM(new_args, 1, items); PyTuple_SET_ITEM(new_args, 1, Py_NewRef(items));
array_obj = array_new(arraytype, new_args, NULL); array_obj = array_new(arraytype, new_args, NULL);
Py_DECREF(new_args); Py_DECREF(new_args);
@ -2219,8 +2211,7 @@ array_array___reduce_ex___impl(arrayobject *self, PyTypeObject *cls,
return NULL; return NULL;
} }
if (dict == NULL) { if (dict == NULL) {
dict = Py_None; dict = Py_NewRef(Py_None);
Py_INCREF(dict);
} }
mformat_code = typecode_to_mformat_code(typecode); mformat_code = typecode_to_mformat_code(typecode);
@ -2572,8 +2563,7 @@ array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
} }
view->buf = (void *)self->ob_item; view->buf = (void *)self->ob_item;
view->obj = (PyObject*)self; view->obj = Py_NewRef(self);
Py_INCREF(self);
if (view->buf == NULL) if (view->buf == NULL)
view->buf = (void *)emptybuf; view->buf = (void *)emptybuf;
view->len = Py_SIZE(self) * self->ob_descr->itemsize; view->len = Py_SIZE(self) * self->ob_descr->itemsize;
@ -2885,8 +2875,7 @@ array_iter(arrayobject *ao)
if (it == NULL) if (it == NULL)
return NULL; return NULL;
Py_INCREF(ao); it->ao = (arrayobject*)Py_NewRef(ao);
it->ao = ao;
it->index = 0; it->index = 0;
it->getitem = ao->ob_descr->getitem; it->getitem = ao->ob_descr->getitem;
PyObject_GC_Track(it); PyObject_GC_Track(it);
@ -3083,8 +3072,8 @@ array_modexec(PyObject *m)
CREATE_TYPE(m, state->ArrayIterType, &arrayiter_spec); CREATE_TYPE(m, state->ArrayIterType, &arrayiter_spec);
Py_SET_TYPE(state->ArrayIterType, &PyType_Type); Py_SET_TYPE(state->ArrayIterType, &PyType_Type);
Py_INCREF((PyObject *)state->ArrayType); if (PyModule_AddObject(m, "ArrayType",
if (PyModule_AddObject(m, "ArrayType", (PyObject *)state->ArrayType) < 0) { Py_NewRef((PyObject *)state->ArrayType)) < 0) {
Py_DECREF((PyObject *)state->ArrayType); Py_DECREF((PyObject *)state->ArrayType);
return -1; return -1;
} }