Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-106104: Use public weakref APIs in sqlite3 #106105

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Modules/_sqlite/blob.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif

#include "blob.h"
#include "util.h"
#include "pycore_weakref.h" // _PyWeakref_GET_REF()

#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
#include "clinic/blob.c.h"
Expand Down Expand Up @@ -102,12 +97,18 @@ pysqlite_close_all_blobs(pysqlite_Connection *self)
{
for (int i = 0; i < PyList_GET_SIZE(self->blobs); i++) {
PyObject *weakref = PyList_GET_ITEM(self->blobs, i);
PyObject *blob = _PyWeakref_GET_REF(weakref);
if (blob == NULL) {
if (!PyWeakref_Check(weakref)) {
continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that blobs contain something which is not a weakref?

}
close_blob((pysqlite_Blob *)blob);
Py_DECREF(blob);
PyObject *blob;
if (PyWeakref_GetRef(weakref, &blob) < 0) {
PyErr_WriteUnraisable((PyObject *)self);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make _PyErr_WriteUnraisableMsg() public, since this one logs a message which is hard to get, it gives no context.

continue;
}
if (blob) {
close_blob((pysqlite_Blob *)blob);
Py_DECREF(blob);
}
}
}

Expand Down
24 changes: 14 additions & 10 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
* 3. This notice may not be removed or altered from any source distribution.
*/

#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif

#include "module.h"
#include "structmember.h" // PyMemberDef
#include "connection.h"
Expand All @@ -33,7 +29,6 @@
#include "blob.h"
#include "prepare_protocol.h"
#include "util.h"
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()

#include <stdbool.h>

Expand Down Expand Up @@ -987,13 +982,22 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
}

for (Py_ssize_t i = 0; i < PyList_Size(self->cursors); i++) {
PyObject* weakref = PyList_GetItem(self->cursors, i);
if (_PyWeakref_IS_DEAD(weakref)) {
PyObject *weakref = PyList_GetItem(self->cursors, i);
if (!PyWeakref_Check(weakref)) {
continue;
}
if (PyList_Append(new_list, weakref) != 0) {
Py_DECREF(new_list);
return;
PyObject *item;
if (PyWeakref_GetRef(weakref, &item) < 0) {
PyErr_WriteUnraisable((PyObject *)self);
continue;
}
if (item) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest if (!item) continue; to avoid one indentation level.

int rc = PyList_Append(new_list, item);
Py_DECREF(item);
if (rc < 0) {
Py_DECREF(new_list);
return;
}
}
}

Expand Down