New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add parse_dsn module function #321
Changes from 1 commit
6c57e4a
6a2f21a
3200cd7
cc08e14
d604127
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,44 @@ psyco_connect(PyObject *self, PyObject *args, PyObject *keywds) | |
return conn; | ||
} | ||
|
||
#define psyco_parse_dsn_doc \ | ||
"parse_dsn(dsn) -- Parse database connection string.\n\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better: parse_dsn(dsn) -> dict. Also I don't think you need the \n's. Even better put all the documentation here and use autodoc to get it in the docs. |
||
|
||
static PyObject * | ||
psyco_parse_dsn(PyObject *self, PyObject *args) | ||
{ | ||
char *dsn, *err; | ||
PQconninfoOption *options = NULL, *o; | ||
PyObject *res = NULL, *value; | ||
|
||
if (!PyArg_ParseTuple(args, "s", &dsn)) { | ||
return NULL; | ||
} | ||
|
||
options = PQconninfoParse(dsn, &err); | ||
if (!options) { | ||
PyErr_Format(PyExc_RuntimeError, "PQconninfoParse: %s: %s", dsn, err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error is wrong. The dsn shouldn't be printed back as it may contain passwords that may end up in the logs. It shoud be a ProgrammingError if the dsn is wrong (with a human error message such as "error parsing the dsn: %s" % err) or an OperationalError if err is null as that's a memory allocation error according to the docs). |
||
PQfreemem(err); | ||
return NULL; | ||
} | ||
|
||
res = PyDict_New(); | ||
for (o = options; o->keyword != NULL; o++) { | ||
if (o->val != NULL) { | ||
value = PyString_FromString(o->val); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it work ok on Python3? Probably you need a function like Text_FromUTF8 -- see psycopg/python.h and example of usage all around the lib. |
||
if (value == NULL || PyDict_SetItemString(res, o->keyword, value) != 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is terrible, separate the error check from PyString_FromString from the SetItemString! It is also a reference leak: SetItemString doesn't steal the reference. value must be decref'd. |
||
Py_DECREF(res); | ||
res = NULL; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
PQconninfoFree(options); | ||
|
||
return res; | ||
} | ||
|
||
/** type registration **/ | ||
#define psyco_register_type_doc \ | ||
"register_type(obj, conn_or_curs) -> None -- register obj with psycopg type system\n\n" \ | ||
|
@@ -695,6 +733,8 @@ psyco_make_description_type(void) | |
static PyMethodDef psycopgMethods[] = { | ||
{"_connect", (PyCFunction)psyco_connect, | ||
METH_VARARGS|METH_KEYWORDS, psyco_connect_doc}, | ||
{"parse_dsn", (PyCFunction)psyco_parse_dsn, | ||
METH_VARARGS, psyco_parse_dsn_doc}, | ||
{"adapt", (PyCFunction)psyco_microprotocols_adapt, | ||
METH_VARARGS, psyco_microprotocols_adapt_doc}, | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should be exposed by psycopg2.extensions, not the main module.