mirror of
https://github.com/python/cpython.git
synced 2025-11-01 02:38:53 +00:00
Issue #20440: Cleaning up the code by using Py_SETREF.
This commit is contained in:
parent
dcf76c9d0a
commit
576f132b98
14 changed files with 39 additions and 120 deletions
|
|
@ -59,15 +59,11 @@ BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||
static int
|
||||
BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
PyObject *tmp;
|
||||
|
||||
if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
|
||||
return -1;
|
||||
|
||||
tmp = self->args;
|
||||
self->args = args;
|
||||
Py_INCREF(self->args);
|
||||
Py_XDECREF(tmp);
|
||||
Py_INCREF(args);
|
||||
Py_SETREF(self->args, args);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -328,11 +324,10 @@ PyException_GetCause(PyObject *self) {
|
|||
|
||||
/* Steals a reference to cause */
|
||||
void
|
||||
PyException_SetCause(PyObject *self, PyObject *cause) {
|
||||
PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
|
||||
((PyBaseExceptionObject *)self)->cause = cause;
|
||||
PyException_SetCause(PyObject *self, PyObject *cause)
|
||||
{
|
||||
((PyBaseExceptionObject *)self)->suppress_context = 1;
|
||||
Py_XDECREF(old_cause);
|
||||
Py_SETREF(((PyBaseExceptionObject *)self)->cause, cause);
|
||||
}
|
||||
|
||||
PyObject *
|
||||
|
|
@ -344,10 +339,9 @@ PyException_GetContext(PyObject *self) {
|
|||
|
||||
/* Steals a reference to context */
|
||||
void
|
||||
PyException_SetContext(PyObject *self, PyObject *context) {
|
||||
PyObject *old_context = ((PyBaseExceptionObject *)self)->context;
|
||||
((PyBaseExceptionObject *)self)->context = context;
|
||||
Py_XDECREF(old_context);
|
||||
PyException_SetContext(PyObject *self, PyObject *context)
|
||||
{
|
||||
Py_SETREF(((PyBaseExceptionObject *)self)->context, context);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -349,15 +349,11 @@ frame_gettrace(PyFrameObject *f, void *closure)
|
|||
static int
|
||||
frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
|
||||
{
|
||||
PyObject* old_value;
|
||||
|
||||
/* We rely on f_lineno being accurate when f_trace is set. */
|
||||
f->f_lineno = PyFrame_GetLineNumber(f);
|
||||
|
||||
old_value = f->f_trace;
|
||||
Py_XINCREF(v);
|
||||
f->f_trace = v;
|
||||
Py_XDECREF(old_value);
|
||||
Py_SETREF(f->f_trace, v);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -249,7 +249,6 @@ func_get_code(PyFunctionObject *op)
|
|||
static int
|
||||
func_set_code(PyFunctionObject *op, PyObject *value)
|
||||
{
|
||||
PyObject *tmp;
|
||||
Py_ssize_t nfree, nclosure;
|
||||
|
||||
/* Not legal to del f.func_code or to set it to anything
|
||||
|
|
@ -270,10 +269,8 @@ func_set_code(PyFunctionObject *op, PyObject *value)
|
|||
nclosure, nfree);
|
||||
return -1;
|
||||
}
|
||||
tmp = op->func_code;
|
||||
Py_INCREF(value);
|
||||
op->func_code = value;
|
||||
Py_DECREF(tmp);
|
||||
Py_SETREF(op->func_code, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -287,8 +284,6 @@ func_get_name(PyFunctionObject *op)
|
|||
static int
|
||||
func_set_name(PyFunctionObject *op, PyObject *value)
|
||||
{
|
||||
PyObject *tmp;
|
||||
|
||||
/* Not legal to del f.func_name or to set it to anything
|
||||
* other than a string object. */
|
||||
if (value == NULL || !PyUnicode_Check(value)) {
|
||||
|
|
@ -296,10 +291,8 @@ func_set_name(PyFunctionObject *op, PyObject *value)
|
|||
"__name__ must be set to a string object");
|
||||
return -1;
|
||||
}
|
||||
tmp = op->func_name;
|
||||
Py_INCREF(value);
|
||||
op->func_name = value;
|
||||
Py_DECREF(tmp);
|
||||
Py_SETREF(op->func_name, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -313,8 +306,6 @@ func_get_qualname(PyFunctionObject *op)
|
|||
static int
|
||||
func_set_qualname(PyFunctionObject *op, PyObject *value)
|
||||
{
|
||||
PyObject *tmp;
|
||||
|
||||
/* Not legal to del f.__qualname__ or to set it to anything
|
||||
* other than a string object. */
|
||||
if (value == NULL || !PyUnicode_Check(value)) {
|
||||
|
|
@ -322,10 +313,8 @@ func_set_qualname(PyFunctionObject *op, PyObject *value)
|
|||
"__qualname__ must be set to a string object");
|
||||
return -1;
|
||||
}
|
||||
tmp = op->func_qualname;
|
||||
Py_INCREF(value);
|
||||
op->func_qualname = value;
|
||||
Py_DECREF(tmp);
|
||||
Py_SETREF(op->func_qualname, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -343,8 +332,6 @@ func_get_defaults(PyFunctionObject *op)
|
|||
static int
|
||||
func_set_defaults(PyFunctionObject *op, PyObject *value)
|
||||
{
|
||||
PyObject *tmp;
|
||||
|
||||
/* Legal to del f.func_defaults.
|
||||
* Can only set func_defaults to NULL or a tuple. */
|
||||
if (value == Py_None)
|
||||
|
|
@ -354,10 +341,8 @@ func_set_defaults(PyFunctionObject *op, PyObject *value)
|
|||
"__defaults__ must be set to a tuple object");
|
||||
return -1;
|
||||
}
|
||||
tmp = op->func_defaults;
|
||||
Py_XINCREF(value);
|
||||
op->func_defaults = value;
|
||||
Py_XDECREF(tmp);
|
||||
Py_SETREF(op->func_defaults, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -375,8 +360,6 @@ func_get_kwdefaults(PyFunctionObject *op)
|
|||
static int
|
||||
func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
|
||||
{
|
||||
PyObject *tmp;
|
||||
|
||||
if (value == Py_None)
|
||||
value = NULL;
|
||||
/* Legal to del f.func_kwdefaults.
|
||||
|
|
@ -386,10 +369,8 @@ func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
|
|||
"__kwdefaults__ must be set to a dict object");
|
||||
return -1;
|
||||
}
|
||||
tmp = op->func_kwdefaults;
|
||||
Py_XINCREF(value);
|
||||
op->func_kwdefaults = value;
|
||||
Py_XDECREF(tmp);
|
||||
Py_SETREF(op->func_kwdefaults, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -408,8 +389,6 @@ func_get_annotations(PyFunctionObject *op)
|
|||
static int
|
||||
func_set_annotations(PyFunctionObject *op, PyObject *value)
|
||||
{
|
||||
PyObject *tmp;
|
||||
|
||||
if (value == Py_None)
|
||||
value = NULL;
|
||||
/* Legal to del f.func_annotations.
|
||||
|
|
@ -420,10 +399,8 @@ func_set_annotations(PyFunctionObject *op, PyObject *value)
|
|||
"__annotations__ must be set to a dict object");
|
||||
return -1;
|
||||
}
|
||||
tmp = op->func_annotations;
|
||||
Py_XINCREF(value);
|
||||
op->func_annotations = value;
|
||||
Py_XDECREF(tmp);
|
||||
Py_SETREF(op->func_annotations, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -510,8 +510,6 @@ gen_get_name(PyGenObject *op)
|
|||
static int
|
||||
gen_set_name(PyGenObject *op, PyObject *value)
|
||||
{
|
||||
PyObject *tmp;
|
||||
|
||||
/* Not legal to del gen.gi_name or to set it to anything
|
||||
* other than a string object. */
|
||||
if (value == NULL || !PyUnicode_Check(value)) {
|
||||
|
|
@ -519,10 +517,8 @@ gen_set_name(PyGenObject *op, PyObject *value)
|
|||
"__name__ must be set to a string object");
|
||||
return -1;
|
||||
}
|
||||
tmp = op->gi_name;
|
||||
Py_INCREF(value);
|
||||
op->gi_name = value;
|
||||
Py_DECREF(tmp);
|
||||
Py_SETREF(op->gi_name, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -536,8 +532,6 @@ gen_get_qualname(PyGenObject *op)
|
|||
static int
|
||||
gen_set_qualname(PyGenObject *op, PyObject *value)
|
||||
{
|
||||
PyObject *tmp;
|
||||
|
||||
/* Not legal to del gen.__qualname__ or to set it to anything
|
||||
* other than a string object. */
|
||||
if (value == NULL || !PyUnicode_Check(value)) {
|
||||
|
|
@ -545,10 +539,8 @@ gen_set_qualname(PyGenObject *op, PyObject *value)
|
|||
"__qualname__ must be set to a string object");
|
||||
return -1;
|
||||
}
|
||||
tmp = op->gi_qualname;
|
||||
Py_INCREF(value);
|
||||
op->gi_qualname = value;
|
||||
Py_DECREF(tmp);
|
||||
Py_SETREF(op->gi_qualname, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1203,7 +1203,7 @@ PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
|
|||
int
|
||||
PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
|
||||
{
|
||||
PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
|
||||
PyObject **dictptr = _PyObject_GetDictPtr(obj);
|
||||
if (dictptr == NULL) {
|
||||
PyErr_SetString(PyExc_AttributeError,
|
||||
"This object has no __dict__");
|
||||
|
|
@ -1219,10 +1219,8 @@ PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
|
|||
"not a '%.200s'", Py_TYPE(value)->tp_name);
|
||||
return -1;
|
||||
}
|
||||
dict = *dictptr;
|
||||
Py_XINCREF(value);
|
||||
*dictptr = value;
|
||||
Py_XDECREF(dict);
|
||||
Py_INCREF(value);
|
||||
Py_SETREF(*dictptr, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2092,7 +2092,7 @@ subtype_dict(PyObject *obj, void *context)
|
|||
static int
|
||||
subtype_setdict(PyObject *obj, PyObject *value, void *context)
|
||||
{
|
||||
PyObject *dict, **dictptr;
|
||||
PyObject **dictptr;
|
||||
PyTypeObject *base;
|
||||
|
||||
base = get_builtin_base_with_dict(Py_TYPE(obj));
|
||||
|
|
@ -2123,10 +2123,8 @@ subtype_setdict(PyObject *obj, PyObject *value, void *context)
|
|||
"not a '%.200s'", Py_TYPE(value)->tp_name);
|
||||
return -1;
|
||||
}
|
||||
dict = *dictptr;
|
||||
Py_XINCREF(value);
|
||||
*dictptr = value;
|
||||
Py_XDECREF(dict);
|
||||
Py_SETREF(*dictptr, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue