Skip to content

Commit 8308c19

Browse files
committed
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.
1 parent 1a1eabf commit 8308c19

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

psycopg/connection_int.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ static cursorObject *
10471047
_conn_get_async_cursor(connectionObject *self) {
10481048
PyObject *py_curs;
10491049

1050-
if (!(py_curs = PyWeakref_GetObject(self->async_cursor))) {
1050+
if (!(py_curs = psyco_weakref_get_object(self->async_cursor))) {
10511051
PyErr_SetString(PyExc_SystemError,
10521052
"got null dereferencing cursor weakref");
10531053
goto error;

psycopg/cursor_type.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ curs_fetchone(cursorObject *self, PyObject *dummy)
779779
successive requests to reallocate it */
780780
if (self->row >= self->rowcount
781781
&& self->conn->async_cursor
782-
&& PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self)
782+
&& psyco_weakref_get_object(self->conn->async_cursor) == (PyObject*)self)
783783
CLEARPGRES(self->pgres);
784784

785785
return res;
@@ -826,7 +826,7 @@ curs_next_named(cursorObject *self)
826826
successive requests to reallocate it */
827827
if (self->row >= self->rowcount
828828
&& self->conn->async_cursor
829-
&& PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self)
829+
&& psyco_weakref_get_object(self->conn->async_cursor) == (PyObject*)self)
830830
CLEARPGRES(self->pgres);
831831

832832
return res;
@@ -911,7 +911,7 @@ curs_fetchmany(cursorObject *self, PyObject *args, PyObject *kwords)
911911
successive requests to reallocate it */
912912
if (self->row >= self->rowcount
913913
&& self->conn->async_cursor
914-
&& PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self)
914+
&& psyco_weakref_get_object(self->conn->async_cursor) == (PyObject*)self)
915915
CLEARPGRES(self->pgres);
916916

917917
/* success */
@@ -980,7 +980,7 @@ curs_fetchall(cursorObject *self, PyObject *dummy)
980980
successive requests to reallocate it */
981981
if (self->row >= self->rowcount
982982
&& self->conn->async_cursor
983-
&& PyWeakref_GetObject(self->conn->async_cursor) == (PyObject*)self)
983+
&& psyco_weakref_get_object(self->conn->async_cursor) == (PyObject*)self)
984984
CLEARPGRES(self->pgres);
985985

986986
/* success */

psycopg/utils.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,36 @@ psyco_get_decimal_type(void)
441441

442442
return decimalType;
443443
}
444+
445+
/* Return the object referred by the weak ref as a borrowed pointer.
446+
*
447+
* Reproduce the semantics of PyWeakref_GetObject(), which was deprecated in
448+
* 3.13.
449+
*
450+
* I know that it would have been better to reproduce the semantics of
451+
* PyWeakref_GetRef(), thank you for the suggestion. However this opens a can
452+
* of worms in cursor_type.c. Why so? Glad you ask: because there are many
453+
* places in that function where we don't check the return value. That stuff is
454+
* convoluted and async: I think in case of failure it would fail of internal
455+
* error, but it's not been reported doing so.
456+
*/
457+
BORROWED PyObject *
458+
psyco_weakref_get_object(PyObject *ref)
459+
{
460+
#if PY_VERSION_HEX >= 0x030d0000
461+
PyObject *obj = NULL;
462+
int rv;
463+
464+
if ((rv = PyWeakref_GetRef(ref, &obj)) > 0) {
465+
Py_DECREF(obj); /* make it weak */
466+
}
467+
else if (rv == 0) { /* dead ref */
468+
obj = Py_None;
469+
}
470+
/* else it's an error */
471+
472+
return obj;
473+
#else
474+
return PyWeakref_GetObject(ref);
475+
#endif
476+
}

psycopg/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ HIDDEN RAISES BORROWED PyObject *psyco_set_error(
5959

6060
HIDDEN PyObject *psyco_get_decimal_type(void);
6161

62+
HIDDEN BORROWED PyObject *psyco_weakref_get_object(PyObject *);
63+
6264
HIDDEN PyObject *Bytes_Format(PyObject *format, PyObject *args);
6365

6466

0 commit comments

Comments
 (0)