diff options
author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2018-12-28 03:36:34 +0100 |
---|---|---|
committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2019-01-21 02:00:04 +0000 |
commit | 117f7d33f87a377008ef706f9e167cf4fcab57bf (patch) | |
tree | 85f7ff8349e00cd800939d9262ddea40e38569cf | |
parent | f9b798aca9822b9b09e0b000e6410261bad3730c (diff) | |
download | psycopg2-117f7d33f87a377008ef706f9e167cf4fcab57bf.tar.gz |
Wrap _Bytes_Resize into a function with clearer semantic
Limit the static checker hacking to a simpler function.
-rw-r--r-- | psycopg/bytes_format.c | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/psycopg/bytes_format.c b/psycopg/bytes_format.c index 040db40..1693969 100644 --- a/psycopg/bytes_format.c +++ b/psycopg/bytes_format.c @@ -99,6 +99,19 @@ getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) return NULL; } +/* wrapper around _Bytes_Resize offering normal Python call semantics */ + +STEALS(1) +Py_LOCAL_INLINE(PyObject *) +resize_bytes(PyObject *b, Py_ssize_t newsize) { + if (0 == _Bytes_Resize(&b, newsize)) { + return b; + } + else { + return NULL; + } +} + /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */ PyObject * @@ -137,10 +150,10 @@ Bytes_Format(PyObject *format, PyObject *args) if (--rescnt < 0) { rescnt = fmtcnt + 100; reslen += rescnt; - if (_Bytes_Resize(&result, reslen)) + if (!(result = resize_bytes(result, reslen))) { return NULL; - res = Bytes_AS_STRING(result) - + reslen - rescnt; + } + res = Bytes_AS_STRING(result) + reslen - rescnt; --rescnt; } *res++ = *fmt++; @@ -248,7 +261,7 @@ Bytes_Format(PyObject *format, PyObject *args) Py_DECREF(args); return PyErr_NoMemory(); } - if (_Bytes_Resize(&result, reslen)) { + if (!(result = resize_bytes(result, reslen))) { Py_XDECREF(temp); if (args_owned) Py_DECREF(args); @@ -281,8 +294,9 @@ Bytes_Format(PyObject *format, PyObject *args) if (args_owned) { Py_DECREF(args); } - if (_Bytes_Resize(&result, reslen - rescnt)) + if (!(result = resize_bytes(result, reslen - rescnt))) { return NULL; + } return result; error: |