Skip to content

Commit 0e832b9

Browse files
committed
Proper type check in prepare() methods for list, binary, qstring
1 parent 76cc838 commit 0e832b9

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

psycopg/adapter_binary.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,14 @@ binary_str(binaryObject *self)
149149
static PyObject *
150150
binary_prepare(binaryObject *self, PyObject *args)
151151
{
152-
connectionObject *conn;
152+
PyObject *conn;
153153

154-
if (!PyArg_ParseTuple(args, "O", &conn))
154+
if (!PyArg_ParseTuple(args, "O!", &connectionType, &conn))
155155
return NULL;
156156

157157
Py_XDECREF(self->conn);
158-
if (conn) {
159-
self->conn = (PyObject*)conn;
160-
Py_INCREF(self->conn);
161-
}
158+
self->conn = conn;
159+
Py_INCREF(self->conn);
162160

163161
Py_INCREF(Py_None);
164162
return Py_None;

psycopg/adapter_list.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ list_getquoted(listObject *self, PyObject *args)
9898
static PyObject *
9999
list_prepare(listObject *self, PyObject *args)
100100
{
101-
connectionObject *conn;
101+
PyObject *conn;
102102

103-
if (!PyArg_ParseTuple(args, "O", &conn))
103+
if (!PyArg_ParseTuple(args, "O!", &connectionType, &conn))
104104
return NULL;
105105

106106
/* note that we don't copy the encoding from the connection, but take a
@@ -109,7 +109,7 @@ list_prepare(listObject *self, PyObject *args)
109109
work even without a connection to the backend. */
110110
Py_CLEAR(self->connection);
111111
Py_INCREF(conn);
112-
self->connection = (PyObject*)conn;
112+
self->connection = conn;
113113

114114
Py_INCREF(Py_None);
115115
return Py_None;

psycopg/adapter_qstring.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,22 @@ qstring_str(qstringObject *self)
124124
static PyObject *
125125
qstring_prepare(qstringObject *self, PyObject *args)
126126
{
127-
connectionObject *conn;
127+
PyObject *conn;
128128

129-
if (!PyArg_ParseTuple(args, "O", &conn))
129+
if (!PyArg_ParseTuple(args, "O!", &connectionType, &conn))
130130
return NULL;
131131

132132
/* we bother copying the encoding only if the wrapped string is unicode,
133133
we don't need the encoding if that's not the case */
134134
if (PyUnicode_Check(self->wrapped)) {
135135
if (self->encoding) free(self->encoding);
136-
self->encoding = strdup(conn->codec);
137-
Dprintf("qstring_prepare: set encoding to %s", conn->codec);
136+
self->encoding = strdup(((connectionObject *)conn)->codec);
137+
Dprintf("qstring_prepare: set encoding to %s", self->encoding);
138138
}
139139

140140
Py_CLEAR(self->conn);
141-
if (conn) {
142-
Py_INCREF(conn);
143-
self->conn = (PyObject*)conn;
144-
}
141+
Py_INCREF(conn);
142+
self->conn = conn;
145143

146144
Py_INCREF(Py_None);
147145
return Py_None;

0 commit comments

Comments
 (0)