Skip to content

Commit db9a415

Browse files
committed
Added TO_STATE() marker to work around cpychecker refcount issue
See davidmalcolm/gcc-python-plugin#109
1 parent da06fb7 commit db9a415

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

psycopg/connection_int.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ conn_tpc_begin(connectionObject *self, xidObject *xid)
13921392

13931393
/* The transaction started ok, let's store this xid. */
13941394
Py_INCREF(xid);
1395-
self->tpc_xid = xid;
1395+
self->tpc_xid = (xidObject *)TO_STATE((PyObject *)xid);
13961396

13971397
return 0;
13981398
}

psycopg/connection_type.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,13 +1337,13 @@ connection_setup(connectionObject *self, const char *dsn, long int async)
13371337
);
13381338

13391339
if (0 > psycopg_strdup(&self->dsn, dsn, -1)) { goto exit; }
1340-
if (!(self->notice_list = PyList_New(0))) { goto exit; }
1341-
if (!(self->notifies = PyList_New(0))) { goto exit; }
1340+
if (!(self->notice_list = TO_STATE(PyList_New(0)))) { goto exit; }
1341+
if (!(self->notifies = TO_STATE(PyList_New(0)))) { goto exit; }
13421342
self->async = async;
13431343
self->status = CONN_STATUS_SETUP;
13441344
self->async_status = ASYNC_DONE;
1345-
if (!(self->string_types = PyDict_New())) { goto exit; }
1346-
if (!(self->binary_types = PyDict_New())) { goto exit; }
1345+
if (!(self->string_types = TO_STATE(PyDict_New()))) { goto exit; }
1346+
if (!(self->binary_types = TO_STATE(PyDict_New()))) { goto exit; }
13471347
self->isolevel = ISOLATION_LEVEL_DEFAULT;
13481348
self->readonly = STATE_DEFAULT;
13491349
self->deferrable = STATE_DEFAULT;

psycopg/utils.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,19 @@ psyco_GetDecimalType(void)
465465

466466
return decimalType;
467467
}
468+
469+
470+
/* Transfer ownership of an object to another object's state.
471+
*
472+
* Work around what seems a bug to the cpychecker which doesn't recognise
473+
* the new reference: tell it one reference is just gone.
474+
*
475+
* See davidmalcolm/gcc-python-plugin#109
476+
*/
477+
#ifdef WITH_CPYCHECKER_RETURNS_BORROWED_REF_ATTRIBUTE
478+
STEALS(1) IGNORE_REFCOUNT BORROWED PyObject *
479+
TO_STATE(PyObject* obj)
480+
{
481+
return obj;
482+
}
483+
#endif

psycopg/utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@ HIDDEN RAISES BORROWED PyObject *psyco_set_error(
5858

5959
HIDDEN PyObject *psyco_GetDecimalType(void);
6060

61+
#ifdef WITH_CPYCHECKER_RETURNS_BORROWED_REF_ATTRIBUTE
62+
HIDDEN STEALS(1) IGNORE_REFCOUNT BORROWED PyObject *TO_STATE(PyObject* obj);
63+
#else
64+
#define TO_STATE(x) x
65+
#endif
66+
6167
#endif /* !defined(UTILS_H) */

0 commit comments

Comments
 (0)