mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-26 10:53:44 +03:00
Use -1 instead of 0 to say "calculate the length" in many funcs
0 is a valid length, isn't it?
This commit is contained in:
parent
a255e4e1c6
commit
dfe547856e
|
@ -178,7 +178,7 @@ qstring_set_encoding(qstringObject *self, PyObject *pyenc)
|
|||
Py_INCREF(pyenc);
|
||||
if (!(pyenc = psycopg_ensure_bytes(pyenc))) { goto exit; }
|
||||
if (!(tmp = Bytes_AsString(pyenc))) { goto exit; }
|
||||
if (0 > psycopg_strdup(&cenc, tmp, 0)) { goto exit; }
|
||||
if (0 > psycopg_strdup(&cenc, tmp, -1)) { goto exit; }
|
||||
|
||||
Dprintf("qstring_set_encoding: encoding set to %s", cenc);
|
||||
PyMem_Free((void *)self->encoding);
|
||||
|
|
|
@ -1097,7 +1097,7 @@ connection_setup(connectionObject *self, const char *dsn, long int async)
|
|||
self, async, Py_REFCNT(self)
|
||||
);
|
||||
|
||||
if (0 > psycopg_strdup(&self->dsn, dsn, 0)) { goto exit; }
|
||||
if (0 > psycopg_strdup(&self->dsn, dsn, -1)) { goto exit; }
|
||||
if (!(self->notice_list = PyList_New(0))) { goto exit; }
|
||||
if (!(self->notifies = PyList_New(0))) { goto exit; }
|
||||
self->async = async;
|
||||
|
|
|
@ -1079,7 +1079,7 @@ psyco_curs_callproc(cursorObject *self, PyObject *args)
|
|||
if (!(cpname = Bytes_AsString(pname))) { goto exit; }
|
||||
|
||||
if (!(scpnames[i] = psycopg_escape_identifier(
|
||||
self->conn, cpname, 0))) {
|
||||
self->conn, cpname, -1))) {
|
||||
Py_CLEAR(pname);
|
||||
goto exit;
|
||||
}
|
||||
|
@ -1457,12 +1457,12 @@ psyco_curs_copy_from(cursorObject *self, PyObject *args, PyObject *kwargs)
|
|||
goto exit;
|
||||
|
||||
if (!(quoted_delimiter = psycopg_escape_string(
|
||||
self->conn, sep, 0, NULL, NULL))) {
|
||||
self->conn, sep, -1, NULL, NULL))) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (!(quoted_null = psycopg_escape_string(
|
||||
self->conn, null, 0, NULL, NULL))) {
|
||||
self->conn, null, -1, NULL, NULL))) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
@ -1551,12 +1551,12 @@ psyco_curs_copy_to(cursorObject *self, PyObject *args, PyObject *kwargs)
|
|||
goto exit;
|
||||
|
||||
if (!(quoted_delimiter = psycopg_escape_string(
|
||||
self->conn, sep, 0, NULL, NULL))) {
|
||||
self->conn, sep, -1, NULL, NULL))) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (!(quoted_null = psycopg_escape_string(
|
||||
self->conn, null, 0, NULL, NULL))) {
|
||||
self->conn, null, -1, NULL, NULL))) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
@ -1899,10 +1899,10 @@ cursor_setup(cursorObject *self, connectionObject *conn, const char *name)
|
|||
Dprintf("cursor_setup: parameters: name = %s, conn = %p", name, conn);
|
||||
|
||||
if (name) {
|
||||
if (0 > psycopg_strdup(&self->name, name, 0)) {
|
||||
if (0 > psycopg_strdup(&self->name, name, -1)) {
|
||||
return -1;
|
||||
}
|
||||
if (!(self->qname = psycopg_escape_identifier(conn, name, 0))) {
|
||||
if (!(self->qname = psycopg_escape_identifier(conn, name, -1))) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -227,7 +227,7 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres)
|
|||
errorObject *perr = (errorObject *)pyerr;
|
||||
|
||||
PyMem_Free(perr->pyenc);
|
||||
psycopg_strdup(&perr->pyenc, conn->pyenc, 0);
|
||||
psycopg_strdup(&perr->pyenc, conn->pyenc, -1);
|
||||
|
||||
Py_CLEAR(perr->pgerror);
|
||||
perr->pgerror = error_text_from_chars(perr, err);
|
||||
|
@ -765,7 +765,7 @@ pq_tpc_command_locked(connectionObject *conn, const char *cmd, const char *tid,
|
|||
PyEval_RestoreThread(*tstate);
|
||||
|
||||
/* convert the xid into the postgres transaction_id and quote it. */
|
||||
if (!(etid = psycopg_escape_string(conn, tid, 0, NULL, NULL)))
|
||||
if (!(etid = psycopg_escape_string(conn, tid, -1, NULL, NULL)))
|
||||
{ goto exit; }
|
||||
|
||||
/* prepare the command to the server */
|
||||
|
|
|
@ -129,7 +129,7 @@ RAISES HIDDEN PyObject *psyco_set_error(PyObject *exc, cursorObject *curs, const
|
|||
HIDDEN char *psycopg_escape_string(connectionObject *conn,
|
||||
const char *from, Py_ssize_t len, char *to, Py_ssize_t *tolen);
|
||||
HIDDEN char *psycopg_escape_identifier(connectionObject *conn,
|
||||
const char *str, size_t len);
|
||||
const char *str, Py_ssize_t len);
|
||||
HIDDEN int psycopg_strdup(char **to, const char *from, Py_ssize_t len);
|
||||
HIDDEN int psycopg_is_text_file(PyObject *f);
|
||||
|
||||
|
|
|
@ -165,7 +165,6 @@ psyco_quote_ident(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
{
|
||||
PyObject *ident = NULL, *obj = NULL, *result = NULL;
|
||||
connectionObject *conn;
|
||||
const char *str;
|
||||
char *quoted = NULL;
|
||||
|
||||
static char *kwlist[] = {"ident", "scope", NULL};
|
||||
|
@ -188,12 +187,9 @@ psyco_quote_ident(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
Py_INCREF(ident); /* for ensure_bytes */
|
||||
if (!(ident = psycopg_ensure_bytes(ident))) { goto exit; }
|
||||
|
||||
str = Bytes_AS_STRING(ident);
|
||||
if (!(quoted = psycopg_escape_identifier(conn,
|
||||
Bytes_AS_STRING(ident), Bytes_GET_SIZE(ident)))) { goto exit; }
|
||||
|
||||
quoted = psycopg_escape_identifier(conn, str, strlen(str));
|
||||
if (!quoted) {
|
||||
goto exit;
|
||||
}
|
||||
result = conn_text_from_chars(conn, quoted);
|
||||
|
||||
exit:
|
||||
|
|
|
@ -40,6 +40,8 @@
|
|||
* and set an exception. The returned string includes quotes and leading E if
|
||||
* needed.
|
||||
*
|
||||
* `len` is optional: if < 0 it will be calculated.
|
||||
*
|
||||
* If tolen is set, it will contain the length of the escaped string,
|
||||
* including quotes.
|
||||
*/
|
||||
|
@ -50,7 +52,7 @@ psycopg_escape_string(connectionObject *conn, const char *from, Py_ssize_t len,
|
|||
Py_ssize_t ql;
|
||||
int eq = (conn && (conn->equote)) ? 1 : 0;
|
||||
|
||||
if (len == 0) {
|
||||
if (len < 0) {
|
||||
len = strlen(from);
|
||||
} else if (strchr(from, '\0') != from + len) {
|
||||
PyErr_Format(PyExc_ValueError, "A string literal cannot contain NUL (0x00) characters.");
|
||||
|
@ -92,13 +94,13 @@ psycopg_escape_string(connectionObject *conn, const char *from, Py_ssize_t len,
|
|||
|
||||
/* Escape a string for inclusion in a query as identifier.
|
||||
*
|
||||
* 'len' is optional: if 0 the length is calculated.
|
||||
* 'len' is optional: if < 0 it will be calculated.
|
||||
*
|
||||
* Return a string allocated by Postgres: free it using PQfreemem
|
||||
* In case of error set a Python exception.
|
||||
*/
|
||||
char *
|
||||
psycopg_escape_identifier(connectionObject *conn, const char *str, size_t len)
|
||||
psycopg_escape_identifier(connectionObject *conn, const char *str, Py_ssize_t len)
|
||||
{
|
||||
char *rv = NULL;
|
||||
|
||||
|
@ -107,7 +109,7 @@ psycopg_escape_identifier(connectionObject *conn, const char *str, size_t len)
|
|||
goto exit;
|
||||
}
|
||||
|
||||
if (!len) { len = strlen(str); }
|
||||
if (len < 0) { len = strlen(str); }
|
||||
|
||||
rv = PQescapeIdentifier(conn->pgconn, str, len);
|
||||
if (!rv) {
|
||||
|
@ -127,7 +129,7 @@ exit:
|
|||
/* Duplicate a string.
|
||||
*
|
||||
* Allocate a new buffer on the Python heap containing the new string.
|
||||
* 'len' is optional: if 0 the length is calculated.
|
||||
* 'len' is optional: if < 0 the length is calculated.
|
||||
*
|
||||
* Store the return in 'to' and return 0 in case of success, else return -1
|
||||
* and raise an exception.
|
||||
|
@ -141,7 +143,7 @@ psycopg_strdup(char **to, const char *from, Py_ssize_t len)
|
|||
*to = NULL;
|
||||
return 0;
|
||||
}
|
||||
if (!len) { len = strlen(from); }
|
||||
if (len < 0) { len = strlen(from); }
|
||||
if (!(*to = PyMem_Malloc(len + 1))) {
|
||||
PyErr_NoMemory();
|
||||
return -1;
|
||||
|
|
Loading…
Reference in New Issue
Block a user