cpython: fd36d72f6030 (original) (raw)
Mercurial > cpython
changeset 99669:fd36d72f6030 3.5
Issue #20440: Massive replacing unsafe attribute setting code with special macro Py_SETREF. [#20440]
line wrap: on
line diff
--- a/Include/object.h
+++ b/Include/object.h
@@ -846,6 +846,32 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject )
Py_DECREF(_py_xdecref_tmp); [](#l1.4)
} while (0)
+#ifndef Py_LIMITED_API
+/ Safely decref op
and set op
to op2
.
- *
- *
Py_XDECREF(op);[](#l1.12)
op = op2;[](#l1.13)
- *
- *
Py_SETREF(op, op2);[](#l1.17)
- *
- */ + +#define Py_SETREF(op, op2) [](#l1.24)
- do { [](#l1.25)
PyObject *_py_tmp = (PyObject *)(op); \[](#l1.26)
(op) = (op2); \[](#l1.27)
Py_XDECREF(_py_tmp); \[](#l1.28)
- } while (0)
+ +#endif /* ifndef Py_LIMITED_API / + / These are provided as conveniences to Python runtime embedders, so that they can have object code that is not dependent on Python compilation flags.
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ Release date: tba Core and Builtins ----------------- +- Issue #20440: Massive replacing unsafe attribute setting code with special
- Issue #25766: Special method bytes() now works in str subclasses.
- Issue #25421: sizeof methods of builtin types now use dynamic basic size.
--- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -276,9 +276,8 @@ static int else { if (PyUnicode_READY(src) == -1) return -1;
Py_XDECREF(*target);[](#l3.7) Py_INCREF(src);[](#l3.8)
*target = src;[](#l3.9)
} return 0; @@ -784,8 +783,7 @@ parse_process_char(ReaderObj *self, Py_U static int parse_reset(ReaderObj *self) {Py_SETREF(*target, src);[](#l3.10) }[](#l3.11)
--- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -391,8 +391,7 @@ StructUnionType_new(PyTypeObject *type, Py_DECREF((PyObject *)dict); return NULL; }
- Py_SETREF(result->tp_dict, (PyObject *)dict); dict->format = _ctypes_alloc_format_string(NULL, "B"); if (dict->format == NULL) { Py_DECREF(result);
@@ -871,8 +870,7 @@ PyCPointerType_SetProto(StgDictObject *s return -1; } Py_INCREF(proto);
@@ -962,8 +960,7 @@ PyCPointerType_new(PyTypeObject *type, P Py_DECREF((PyObject *)stgdict); return NULL; }
return (PyObject *)result; } @@ -1406,8 +1403,7 @@ PyCArrayType_new(PyTypeObject type, PyO / replace the class dict by our updated spam dict */ if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) goto error;
- Py_SETREF(result->tp_dict, (PyObject )stgdict); / steal the reference / stgdict = NULL; / Special case for character arrays. @@ -1820,8 +1816,7 @@ static PyObject *CreateSwappedType(PyTyp Py_DECREF((PyObject *)stgdict); return NULL; }
return (PyObject *)result; } @@ -1949,8 +1944,7 @@ PyCSimpleType_new(PyTypeObject *type, Py Py_DECREF((PyObject *)stgdict); return NULL; }
/* Install from_param class methods in ctypes base classes. Overrides the PyCSimpleType_from_param generic method. @@ -2313,8 +2307,7 @@ PyCFuncPtrType_new(PyTypeObject *type, P Py_DECREF((PyObject *)stgdict); return NULL; }
if (-1 == make_funcptrtype_dict(stgdict)) { Py_DECREF(result); @@ -2458,8 +2451,7 @@ KeepRef(CDataObject *target, Py_ssize_t return -1; } if (ob->b_objects == NULL || !PyDict_CheckExact(ob->b_objects)) {
Py_XDECREF(ob->b_objects);[](#l4.77)
ob->b_objects = keep; /* refcount consumed */[](#l4.78)
} key = unique_key(target, index); @@ -2962,9 +2954,8 @@ PyCFuncPtr_set_errcheck(PyCFuncPtrObject "the errcheck attribute must be callable"); return -1; }Py_SETREF(ob->b_objects, keep); /* refcount consumed */[](#l4.79) return 0;[](#l4.80)
@@ -2993,9 +2984,8 @@ PyCFuncPtr_set_restype(PyCFuncPtrObject return -1; } Py_XDECREF(self->checker);
- Py_SETREF(self->restype, ob); self->checker = PyObject_GetAttrString(ob, "check_retval"); if (self->checker == NULL) PyErr_Clear();
@@ -3033,11 +3023,9 @@ PyCFuncPtr_set_argtypes(PyCFuncPtrObject converters = converters_from_argtypes(ob); if (!converters) return -1;
Py_XDECREF(self->converters);[](#l4.109)
self->converters = converters;[](#l4.110)
Py_XDECREF(self->argtypes);[](#l4.111)
Py_SETREF(self->converters, converters);[](#l4.112) Py_INCREF(ob);[](#l4.113)
self->argtypes = ob;[](#l4.114)
} return 0; } @@ -5164,9 +5152,8 @@ comerror_init(PyObject *self, PyObject * return -1;Py_SETREF(self->argtypes, ob);[](#l4.115)
bself = (PyBaseExceptionObject *)self;
--- a/Modules/_curses_panel.c +++ b/Modules/_curses_panel.c @@ -312,9 +312,8 @@ PyCursesPanel_replace_panel(PyCursesPane PyErr_SetString(_curses_panelstate_global->PyCursesError, "replace_panel() returned ERR"); return NULL; }
--- a/Modules/_io/bytesio.c +++ b/Modules/_io/bytesio.c @@ -969,8 +969,7 @@ static int if (initvalue && initvalue != Py_None) { if (PyBytes_CheckExact(initvalue)) { Py_INCREF(initvalue);
Py_XDECREF(self->buf);[](#l6.7)
self->buf = initvalue;[](#l6.8)
Py_SETREF(self->buf, initvalue);[](#l6.9) self->string_size = PyBytes_GET_SIZE(initvalue);[](#l6.10) }[](#l6.11) else {[](#l6.12)
--- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -204,8 +204,8 @@ void pysqlite_flush_statement_cache(pysq node = node->next; }
- Py_DECREF(self->statement_cache);
- self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
- Py_SETREF(self->statement_cache,
Py_DECREF(self); self->statement_cache->decref_factory = 0; } @@ -318,9 +318,8 @@ PyObject* pysqlite_connection_cursor(pys _pysqlite_drop_unused_cursor_references(self); if (cursor && self->row_factory != Py_None) {(pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self));[](#l7.10)
Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);[](#l7.18) Py_INCREF(self->row_factory);[](#l7.19)
((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;[](#l7.20)
} return cursor; @@ -795,8 +794,7 @@ static void _pysqlite_drop_unused_statem } }Py_SETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);[](#l7.21)
} static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self) @@ -827,8 +825,7 @@ static void _pysqlite_drop_unused_cursor } }
} PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
--- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -170,8 +170,7 @@ int pysqlite_build_row_cast_map(pysqlite return 0; }
for (i = 0; i < sqlite3_column_count(self->statement->st); i++) { converter = NULL; @@ -510,9 +509,8 @@ PyObject* _pysqlite_query_execute(pysqli goto error; /* reset description and rowcount */
- Py_SETREF(self->description, Py_None); self->rowcount = -1L; func_args = PyTuple_New(1); @@ -537,8 +535,8 @@ PyObject* _pysqlite_query_execute(pysqli } if (self->statement->in_use) {
Py_DECREF(self->statement);[](#l8.28)
self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);[](#l8.29)
Py_SETREF(self->statement,[](#l8.30)
PyObject_New(pysqlite_Statement, &pysqlite_StatementType));[](#l8.31) if (!self->statement) {[](#l8.32) goto error;[](#l8.33) }[](#l8.34)
@@ -654,8 +652,7 @@ PyObject* _pysqlite_query_execute(pysqli numcols = sqlite3_column_count(self->statement->st); Py_END_ALLOW_THREADS
Py_DECREF(self->description);[](#l8.39)
self->description = PyTuple_New(numcols);[](#l8.40)
Py_SETREF(self->description, PyTuple_New(numcols));[](#l8.41) if (!self->description) {[](#l8.42) goto error;[](#l8.43) }[](#l8.44)
--- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -756,8 +756,7 @@ deepcopy(PyObject** object, PyObject* me if (!copy) return 0;
--- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -1589,8 +1589,7 @@ static int PySSL_set_context(PySSLSocket return -1; #else Py_INCREF(value);
Py_DECREF(self->ctx);[](#l10.7)
self->ctx = (PySSLContext *) value;[](#l10.8)
Py_SETREF(self->ctx, (PySSLContext *)value);[](#l10.9) SSL_set_SSL_CTX(self->ssl, self->ctx->ctx);[](#l10.10)
#endif } else { @@ -1647,8 +1646,7 @@ PySSL_get_owner(PySSLSocket *self, void static int PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c) {
- Py_SETREF(self->owner, PyWeakref_NewRef(value, NULL)); if (self->owner == NULL) return -1; return 0;
--- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -380,9 +380,8 @@ faulthandler_enable(PyObject *self, PyOb if (tstate == NULL) return NULL;
- Py_SETREF(fatal_error.file, file); fatal_error.fd = fd; fatal_error.all_threads = all_threads; fatal_error.interp = tstate->interp; @@ -599,9 +598,8 @@ faulthandler_dump_traceback_later(PyObje /* Cancel previous thread, if running */ cancel_dump_traceback_later();
- Py_SETREF(thread.file, file); thread.fd = fd; thread.timeout_us = timeout_us; thread.repeat = repeat; @@ -778,9 +776,8 @@ faulthandler_register_py(PyObject *self, user->previous = previous; }
--- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -636,8 +636,7 @@ tee_next(teeobject *to) link = teedataobject_jumplink(to->dataobj); if (link == NULL) return NULL;
Py_DECREF(to->dataobj);[](#l12.7)
to->dataobj = (teedataobject *)link;[](#l12.8)
} value = teedataobject_getitem(to->dataobj, to->index);Py_SETREF(to->dataobj, (teedataobject *)link);[](#l12.9) to->index = 0;[](#l12.10)
--- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -1266,8 +1266,7 @@ PyInit__signal(void) if (Handlers[SIGINT].func == DefaultHandler) { /* Install default int handler */ Py_INCREF(IntHandler);
Py_DECREF(Handlers[SIGINT].func);[](#l13.7)
Handlers[SIGINT].func = IntHandler;[](#l13.8)
}Py_SETREF(Handlers[SIGINT].func, IntHandler);[](#l13.9) old_siginthandler = PyOS_setsig(SIGINT, signal_handler);[](#l13.10)
--- a/Modules/zipimport.c +++ b/Modules/zipimport.c @@ -155,8 +155,7 @@ zipimporter_init(ZipImporter *self, PyOb tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP); if (tmp == NULL) goto error;
Py_DECREF(self->prefix);[](#l14.7)
self->prefix = tmp;[](#l14.8)
--- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -667,8 +667,7 @@ save_unconsumed_input(compobject *self, PyBytes_AS_STRING(self->unused_data), old_size); Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size, self->zst.next_in, self->zst.avail_in);
Py_DECREF(self->unused_data);[](#l15.7)
self->unused_data = new_data;[](#l15.8)
} @@ -680,8 +679,7 @@ save_unconsumed_input(compobject *self, (char *)self->zst.next_in, self->zst.avail_in); if (new_data == NULL) return -1;Py_SETREF(self->unused_data, new_data);[](#l15.9) self->zst.avail_in = 0;[](#l15.10) }[](#l15.11)
Py_DECREF(self->unconsumed_tail);[](#l15.17)
self->unconsumed_tail = new_data;[](#l15.18)
--- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -3520,8 +3520,7 @@ PyBytes_Concat(PyObject **pv, PyObject / Multiple references, need to create new object */ PyObject *v; v = bytes_concat(*pv, w);
Py_DECREF(*pv);[](#l16.7)
*pv = v;[](#l16.8)
--- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -1509,8 +1509,7 @@ property_init(PyObject *self, PyObject * PyObject *get_doc = PyObject_GetAttrId(get, &PyId___doc_); if (get_doc) { if (Py_TYPE(self) == &PyProperty_Type) {
Py_XDECREF(prop->prop_doc);[](#l17.7)
prop->prop_doc = get_doc;[](#l17.8)
Py_SETREF(prop->prop_doc, get_doc);[](#l17.9) }[](#l17.10) else {[](#l17.11) /* If this is a property subclass, put __doc__[](#l17.12)
--- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -236,8 +236,7 @@ BaseException_set_tb(PyBaseExceptionObje } Py_XINCREF(tb);
--- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -857,8 +857,7 @@ dict_to_map(PyObject *map, Py_ssize_t nm } } else if (values[j] != value) { Py_XINCREF(value);
Py_XDECREF(values[j]);[](#l19.7)
values[j] = value;[](#l19.8)
--- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -127,8 +127,7 @@ PyFunction_SetDefaults(PyObject *op, PyO PyErr_SetString(PyExc_SystemError, "non-tuple default args"); return -1; }
- Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
- ((PyFunctionObject *) op) -> func_defaults = defaults;
@@ -159,8 +158,7 @@ PyFunction_SetKwDefaults(PyObject *op, P "non-dict keyword only default args"); return -1; }
- Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
- ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
@@ -192,8 +190,7 @@ PyFunction_SetClosure(PyObject *op, PyOb closure->ob_type->tp_name); return -1; }
- Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
- ((PyFunctionObject *) op) -> func_closure = closure;
@@ -224,8 +221,7 @@ PyFunction_SetAnnotations(PyObject *op, "non-dict annotations"); return -1; }
- Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
- ((PyFunctionObject *) op) -> func_annotations = annotations;
@@ -531,8 +527,7 @@ func_new(PyTypeObject* type, PyObject* a if (name != Py_None) { Py_INCREF(name);
Py_DECREF(newfunc->func_name);[](#l20.47)
newfunc->func_name = name;[](#l20.48)
--- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -69,8 +69,7 @@ module_init_dict(PyModuleObject *mod, Py return -1; if (PyUnicode_CheckExact(name)) { Py_INCREF(name);
Py_XDECREF(mod->md_name);[](#l21.7)
mod->md_name = name;[](#l21.8)
--- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -1066,8 +1066,7 @@ longrangeiter_next(longrangeiterobject * result = PyNumber_Add(r->start, product); Py_DECREF(product); if (result) {
Py_DECREF(r->index);[](#l22.7)
r->index = new_index;[](#l22.8)
--- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -315,9 +315,8 @@ assign_version_tag(PyTypeObject *type) are borrowed reference */ for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) { method_cache[i].value = NULL;
Py_XDECREF(method_cache[i].name);[](#l23.7)
method_cache[i].name = Py_None;[](#l23.8) Py_INCREF(Py_None);[](#l23.9)
Py_SETREF(method_cache[i].name, Py_None);[](#l23.10) }[](#l23.11) /* mark all version tags as invalid */[](#l23.12) PyType_Modified(&PyBaseObject_Type);[](#l23.13)
@@ -462,8 +461,7 @@ type_set_qualname(PyTypeObject type, Py et = (PyHeapTypeObject)type; Py_INCREF(value);
@@ -2910,8 +2908,7 @@ PyObject * else method_cache_misses++; #endif
Py_DECREF(method_cache[h].name);[](#l23.28)
method_cache[h].name = name;[](#l23.29)
--- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1665,8 +1665,7 @@ unicode_resize(PyObject **p_unicode, Py_ _Py_INCREF_UNICODE_EMPTY(); if (!unicode_empty) return -1;
Py_DECREF(*p_unicode);[](#l24.7)
*p_unicode = unicode_empty;[](#l24.8)
} @@ -1674,8 +1673,7 @@ unicode_resize(PyObject **p_unicode, Py_ PyObject *copy = resize_copy(unicode, length); if (copy == NULL) return -1;Py_SETREF(*p_unicode, unicode_empty);[](#l24.9) return 0;[](#l24.10)
Py_DECREF(*p_unicode);[](#l24.17)
*p_unicode = copy;[](#l24.18)
} @@ -13322,8 +13320,7 @@ int return -1; _PyUnicode_FastCopyCharacters(newbuffer, 0, writer->buffer, 0, writer->pos);Py_SETREF(*p_unicode, copy);[](#l24.19) return 0;[](#l24.20)
Py_DECREF(writer->buffer);[](#l24.27)
writer->buffer = newbuffer;[](#l24.28)
} _PyUnicodeWriter_Update(writer); return 0; @@ -15009,8 +15006,7 @@ PyUnicode_InternInPlace(PyObject **p) if (t) { Py_INCREF(t);Py_SETREF(writer->buffer, newbuffer);[](#l24.29)
Py_DECREF(*p);[](#l24.37)
*p = t;[](#l24.38)
--- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -524,9 +524,8 @@ fp_setreadl(struct tok_state *tok, const if (stream == NULL) goto cleanup;
- Py_XDECREF(tok->decoding_readline); readline = _PyObject_GetAttrId(stream, &PyId_readline);
- tok->decoding_readline = readline;
- Py_SETREF(tok->decoding_readline, readline); if (pos > 0) { if (PyObject_CallObject(readline, NULL) == NULL) { readline = NULL;
--- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -680,8 +680,7 @@ setup_context(Py_ssize_t stack_level, Py goto handle_error; } else if (!is_true) {
Py_DECREF(*filename);[](#l26.7)
*filename = PyUnicode_FromString("__main__");[](#l26.8)
Py_SETREF(*filename, PyUnicode_FromString("__main__"));[](#l26.9) if (*filename == NULL)[](#l26.10) goto handle_error;[](#l26.11) }[](#l26.12)
--- a/Python/ceval.c +++ b/Python/ceval.c @@ -3214,8 +3214,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int Py_INCREF(self); func = PyMethod_GET_FUNCTION(func); Py_INCREF(func);
Py_DECREF(*pfunc);[](#l27.7)
*pfunc = self;[](#l27.8)
Py_SETREF(*pfunc, self);[](#l27.9) na++;[](#l27.10) /* n++; */[](#l27.11) } else[](#l27.12)
@@ -4670,8 +4669,7 @@ call_function(PyObject ***pp_stack, int Py_INCREF(self); func = PyMethod_GET_FUNCTION(func); Py_INCREF(func);
Py_DECREF(*pfunc);[](#l27.17)
*pfunc = self;[](#l27.18)
Py_SETREF(*pfunc, self);[](#l27.19) na++;[](#l27.20) n++;[](#l27.21) } else[](#l27.22)
--- a/Python/compile.c +++ b/Python/compile.c @@ -1795,8 +1795,7 @@ compiler_class(struct compiler c, stmt_ { / use the class name for name mangling */ Py_INCREF(s->v.ClassDef.name);
Py_XDECREF(c->u->u_private);[](#l28.7)
c->u->u_private = s->v.ClassDef.name;[](#l28.8)
Py_SETREF(c->u->u_private, s->v.ClassDef.name);[](#l28.9) /* load (global) __name__ ... */[](#l28.10) str = PyUnicode_InternFromString("__name__");[](#l28.11) if (!str || !compiler_nameop(c, str, Load)) {[](#l28.12)