fix: drop warning about the use of deprecated PyWeakref_GetObject function

Not the best fix but a better one (to use PyWeakref_GetRef) would be
more invasive than what I want to do.
This commit is contained in:
Daniele Varrazzo 2025-10-10 00:59:31 +02:00
parent 1a1eabf098
commit 8308c19d6a
4 changed files with 40 additions and 5 deletions

View File

@ -1047,7 +1047,7 @@ static cursorObject *
_conn_get_async_cursor(connectionObject *self) {
PyObject *py_curs;
if (!(py_curs = PyWeakref_GetObject(self->async_cursor))) {
if (!(py_curs = psyco_weakref_get_object(self->async_cursor))) {
PyErr_SetString(PyExc_SystemError,
"got null dereferencing cursor weakref");
goto error;

View File

@ -779,7 +779,7 @@ curs_fetchone(cursorObject *self, PyObject *dummy)
successive requests to reallocate it */
if (self->row >= self->rowcount
&& self->conn->async_cursor
&& PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self)
&& psyco_weakref_get_object(self->conn->async_cursor) == (PyObject*)self)
CLEARPGRES(self->pgres);
return res;
@ -826,7 +826,7 @@ curs_next_named(cursorObject *self)
successive requests to reallocate it */
if (self->row >= self->rowcount
&& self->conn->async_cursor
&& PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self)
&& psyco_weakref_get_object(self->conn->async_cursor) == (PyObject*)self)
CLEARPGRES(self->pgres);
return res;
@ -911,7 +911,7 @@ curs_fetchmany(cursorObject *self, PyObject *args, PyObject *kwords)
successive requests to reallocate it */
if (self->row >= self->rowcount
&& self->conn->async_cursor
&& PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self)
&& psyco_weakref_get_object(self->conn->async_cursor) == (PyObject*)self)
CLEARPGRES(self->pgres);
/* success */
@ -980,7 +980,7 @@ curs_fetchall(cursorObject *self, PyObject *dummy)
successive requests to reallocate it */
if (self->row >= self->rowcount
&& self->conn->async_cursor
&& PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self)
&& psyco_weakref_get_object(self->conn->async_cursor) == (PyObject*)self)
CLEARPGRES(self->pgres);
/* success */

View File

@ -441,3 +441,36 @@ psyco_get_decimal_type(void)
return decimalType;
}
/* Return the object referred by the weak ref as a borrowed pointer.
*
* Reproduce the semantics of PyWeakref_GetObject(), which was deprecated in
* 3.13.
*
* I know that it would have been better to reproduce the semantics of
* PyWeakref_GetRef(), thank you for the suggestion. However this opens a can
* of worms in cursor_type.c. Why so? Glad you ask: because there are many
* places in that function where we don't check the return value. That stuff is
* convoluted and async: I think in case of failure it would fail of internal
* error, but it's not been reported doing so.
*/
BORROWED PyObject *
psyco_weakref_get_object(PyObject *ref)
{
#if PY_VERSION_HEX >= 0x030d0000
PyObject *obj = NULL;
int rv;
if ((rv = PyWeakref_GetRef(ref, &obj)) > 0) {
Py_DECREF(obj); /* make it weak */
}
else if (rv == 0) { /* dead ref */
obj = Py_None;
}
/* else it's an error */
return obj;
#else
return PyWeakref_GetObject(ref);
#endif
}

View File

@ -59,6 +59,8 @@ HIDDEN RAISES BORROWED PyObject *psyco_set_error(
HIDDEN PyObject *psyco_get_decimal_type(void);
HIDDEN BORROWED PyObject *psyco_weakref_get_object(PyObject *);
HIDDEN PyObject *Bytes_Format(PyObject *format, PyObject *args);