Skip to content

Commit

Permalink
do incref for pre 3.13
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 committed Sep 23, 2024
1 parent 7848649 commit 76df344
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 18 deletions.
3 changes: 2 additions & 1 deletion source/http_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ static void s_on_stream_complete(struct aws_http_stream *native_stream, int erro

/* DECREF python self, we don't need to force it to stay alive any longer. */
PyObject *self = aws_py_weakref_get_ref(stream->self_proxy);
/* DECREF twice because `aws_py_weakref_get_ref` returns a strong reference */
Py_XDECREF(self);
Py_XDECREF(self);
aws_py_weakref_release_ref(self);

PyGILState_Release(state);
/*************** GIL RELEASE ***************/
Expand Down
9 changes: 1 addition & 8 deletions source/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,18 +525,11 @@ PyObject *aws_py_weakref_get_ref(PyObject *object) {
#else
/* PyWeakref_GetObject is deprecated since python 3.13 */
self = PyWeakref_GetObject(object); /* borrowed reference */
Py_XINCREF(self);
#endif
return self;
}

void aws_py_weakref_release_ref(PyObject *object) {
(void)object;
/* Python versions before 3.13 returns a borrowed reference */
#if PY_VERSION_HEX >= 0x030D0000
Py_XDECREF(object);
#endif
}

int aws_py_gilstate_ensure(PyGILState_STATE *out_state) {
if (AWS_LIKELY(Py_IsInitialized())) {
*out_state = PyGILState_Ensure();
Expand Down
4 changes: 1 addition & 3 deletions source/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,8 @@ PyObject *aws_py_get_error_message(PyObject *self, PyObject *args);
PyObject *aws_py_memory_view_from_byte_buffer(struct aws_byte_buf *buf);

/* Python 3.13+ changed the function to get a reference from WeakRef. This function is an abstraction over two different
* APIs since we support Python versions before 3.13.*/
* APIs since we support Python versions before 3.13. Returns a strong reference if non-null, which you must release. */
PyObject *aws_py_weakref_get_ref(PyObject *object);
/* Release the weakwef provided by the `aws_py_weakref_get_ref`. */
void aws_py_weakref_release_ref(PyObject *object);

/* Allocator that calls into PyObject_[Malloc|Free|Realloc] */
struct aws_allocator *aws_py_get_allocator(void);
Expand Down
12 changes: 6 additions & 6 deletions source/mqtt_client_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static void s_on_connection_success(
}
}

aws_py_weakref_release_ref(self);
Py_XDECREF(self);
PyGILState_Release(state);
}

Expand All @@ -178,7 +178,7 @@ static void s_on_connection_failure(struct aws_mqtt_client_connection *connectio
}
}

aws_py_weakref_release_ref(self);
Py_XDECREF(self);
PyGILState_Release(state);
}

Expand Down Expand Up @@ -206,7 +206,7 @@ static void s_on_connection_interrupted(struct aws_mqtt_client_connection *conne
}
}

aws_py_weakref_release_ref(self);
Py_XDECREF(self);
PyGILState_Release(state);
}

Expand Down Expand Up @@ -240,7 +240,7 @@ static void s_on_connection_resumed(
PyErr_WriteUnraisable(PyErr_Occurred());
}
}
aws_py_weakref_release_ref(self);
Py_XDECREF(self);
PyGILState_Release(state);
}

Expand Down Expand Up @@ -271,7 +271,7 @@ static void s_on_connection_closed(
}
}

aws_py_weakref_release_ref(self);
Py_XDECREF(self);
PyGILState_Release(state);
}

Expand Down Expand Up @@ -598,7 +598,7 @@ static void s_ws_handshake_transform(
done:;
/* Save off error code, so it doesn't got stomped before we pass it to callback*/
int error_code = aws_last_error();
aws_py_weakref_release_ref(connection_py);
Py_XDECREF(connection_py);

if (ws_transform_capsule) {
Py_DECREF(ws_transform_capsule);
Expand Down

0 comments on commit 76df344

Please sign in to comment.