From af82781b287ac3ed9f169f6e6302daf0aa6a7550 Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Tue, 24 Sep 2024 16:17:36 -0700 Subject: [PATCH] remove AWS_FATAL_ASSERT I'm worried about introducing crashes --- source/module.c | 8 ++++---- source/module.h | 11 ++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/source/module.c b/source/module.c index 7aa0502b1..ff5bb4f28 100644 --- a/source/module.c +++ b/source/module.c @@ -526,7 +526,7 @@ PyObject *aws_py_weakref_get_ref(PyObject *ref) { PyObject *obj = NULL; if (PyWeakref_GetRef(ref, &obj) == -1) { PyErr_WriteUnraisable(PyErr_Occurred()); - AWS_FATAL_ASSERT(0 && "expected a weakref"); + AWS_ASSERT(0 && "expected a weakref"); } return obj; @@ -538,14 +538,14 @@ PyObject *aws_py_weakref_get_ref(PyObject *ref) { PyObject *obj = PyWeakref_GetObject(ref); /* borrowed reference */ if (obj == NULL) { PyErr_WriteUnraisable(PyErr_Occurred()); - AWS_FATAL_ASSERT(0 && "expected a weakref"); + AWS_ASSERT(0 && "expected a weakref"); } else if (obj == Py_None) { - return NULL; + obj = NULL; } else { /* Be like PyWeakref_GetRef() and make it new strong reference */ Py_INCREF(obj); - return obj; } + return obj; #endif } diff --git a/source/module.h b/source/module.h index ca73f2a5b..f626cb1e5 100644 --- a/source/module.h +++ b/source/module.h @@ -113,16 +113,17 @@ PyObject *aws_py_memory_view_from_byte_buffer(struct aws_byte_buf *buf); * Given a weak reference, returns a NEW strong reference to the referenced object, * or NULL if the reference is dead (this function NEVER raises a python exception or AWS Error). * + * You MUST NOT call this if ref came from a user, or ref is NULL. + * * This is a simplified version of PyWeakref_GetRef() / PyWeakref_GetObject(). * Simpler because: * - Python 3.13 adds PyWeakref_GetRef() and deprecates PyWeakref_GetObject(). * This function calls the appropriate one. * - * - This will AWS_FATAL_ASSERT if ref is not a weak reference, - * So you only need to handle 2 outcomes instead of 3 - * (the 3rd being a Python exception for calling it incorrectly). - * But this means it's only safe to call if we created the ref ourselves. - * Do not call if ref could have come from the user. + * - This functions has 2 outcomes instead of 3: + * The 3rd being a Python exception for calling it incorrectly. + * If that happens, this function calls PyErr_WriteUnraisable() to clear the exception, + * which is what you would have done anyway. */ PyObject *aws_py_weakref_get_ref(PyObject *ref);