From ec865ae932e859883e19a06bc399572f3021a347 Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Fri, 1 Sep 2006 08:29:30 +0000 Subject: [PATCH] .connect() port parameter as string or int (closes: #120). --- ChangeLog | 4 ++++ psycopg/psycopgmodule.c | 24 +++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5d89c7b7..26b951e0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2006-09-01 Federico Di Gregorio + * psycopg/psycopgmodule.c: applied patch from jdahlin (#120) + to have .connect() accept either a string or int as the port + parameter. + * psycopg/adapter_binary.c: applied patch from jdahlin (#119) to fix the segfault on empty binary buffers. diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 32d70b2b..0c36ba6b 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -124,6 +124,7 @@ static PyObject * psyco_connect(PyObject *self, PyObject *args, PyObject *keywds) { PyObject *conn, *factory = NULL; + PyObject *pyport = NULL; int idsn=-1, iport=-1; char *dsn=NULL, *database=NULL, *user=NULL, *password=NULL; @@ -134,15 +135,28 @@ psyco_connect(PyObject *self, PyObject *args, PyObject *keywds) "user", "password", "sslmode", "connection_factory", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, keywds, "|sssisssO", kwlist, - &dsn, &database, &host, &iport, + if (!PyArg_ParseTupleAndKeywords(args, keywds, "|sssOsssO", kwlist, + &dsn, &database, &host, &pyport, &user, &password, &sslmode, &factory)) { return NULL; } - if (iport > 0) - PyOS_snprintf(port, 16, "%d", iport); - + if (pyport && PyString_Check(pyport)) { + PyObject *pyint = PyInt_FromString(PyString_AsString(pyport), NULL, 10); + if (!pyint) return NULL; + iport = PyInt_AsLong(pyint); + } + else if (pyport && PyInt_Check(pyport)) { + iport = PyInt_AsLong(pyport); + } + else if (pyport != NULL) { + PyErr_SetString(PyExc_TypeError, "port must be a string or int"); + return NULL; + } + + if (iport > 0) + PyOS_snprintf(port, 16, "%d", iport); + if (dsn == NULL) { int l = 45; /* len("dbname= user= password= host= port= sslmode=\0") */