diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-12-24 08:39:57 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-12-24 08:39:57 (GMT) |
commit | f0069403518243e37da0aaaa1148d9dfee1adebd (patch) | |
tree | c6fc0eb9f3dc2b917e2f998cb25e59248453d49d /Include/object.h | |
parent | 2bd58e39918d83c639366c69a4da247238f8183f (diff) | |
parent | 5a57ade58ec5bee85db41b8ce1340ff077781b65 (diff) | |
download | cpython-f0069403518243e37da0aaaa1148d9dfee1adebd.zip cpython-f0069403518243e37da0aaaa1148d9dfee1adebd.tar.gz cpython-f0069403518243e37da0aaaa1148d9dfee1adebd.tar.bz2 |
Issue #20440: Massive replacing unsafe attribute setting code with special
macro Py_SETREF.
Diffstat (limited to 'Include/object.h')
-rw-r--r-- | Include/object.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Include/object.h b/Include/object.h index 4d286ef..eac9541 100644 --- a/Include/object.h +++ b/Include/object.h @@ -846,6 +846,32 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject *); Py_DECREF(_py_xdecref_tmp); \ } while (0) +#ifndef Py_LIMITED_API +/* Safely decref `op` and set `op` to `op2`. + * + * As in case of Py_CLEAR "the obvious" code can be deadly: + * + * Py_XDECREF(op); + * op = op2; + * + * The safe way is: + * + * Py_SETREF(op, op2); + * + * That arranges to set `op` to `op2` _before_ decref'ing, so that any code + * triggered as a side-effect of `op` getting torn down no longer believes + * `op` points to a valid object. + */ + +#define Py_SETREF(op, op2) \ + do { \ + PyObject *_py_tmp = (PyObject *)(op); \ + (op) = (op2); \ + Py_XDECREF(_py_tmp); \ + } 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. |