mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-03-03 15:45:46 +03:00
Function psycopg_ensure_bytes converted in a "filter" stealing a ref.
This commit is contained in:
parent
3214c23f51
commit
f6fefbea64
|
@ -59,7 +59,6 @@ _pydatetime_string_date_time(pydatetimeObject *self)
|
||||||
{
|
{
|
||||||
PyObject *rv = NULL;
|
PyObject *rv = NULL;
|
||||||
PyObject *iso = NULL;
|
PyObject *iso = NULL;
|
||||||
PyObject *biso = NULL;
|
|
||||||
PyObject *tz;
|
PyObject *tz;
|
||||||
|
|
||||||
/* Select the right PG type to cast into. */
|
/* Select the right PG type to cast into. */
|
||||||
|
@ -79,22 +78,17 @@ _pydatetime_string_date_time(pydatetimeObject *self)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(iso = PyObject_CallMethod(self->wrapped, "isoformat", NULL))) {
|
if (!(iso = psycopg_ensure_bytes(
|
||||||
|
PyObject_CallMethod(self->wrapped, "isoformat", NULL)))) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(biso = psycopg_ensure_bytes(iso))) {
|
rv = Bytes_FromFormat(fmt, Bytes_AsString(iso));
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
rv = Bytes_FromFormat(fmt, Bytes_AsString(biso));
|
|
||||||
|
|
||||||
Py_DECREF(biso);
|
|
||||||
Py_DECREF(iso);
|
Py_DECREF(iso);
|
||||||
return rv;
|
return rv;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
Py_XDECREF(biso);
|
|
||||||
Py_XDECREF(iso);
|
Py_XDECREF(iso);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
|
@ -239,7 +239,6 @@ conn_encoding_to_codec(const char *enc)
|
||||||
char *tmp;
|
char *tmp;
|
||||||
Py_ssize_t size;
|
Py_ssize_t size;
|
||||||
PyObject *pyenc = NULL;
|
PyObject *pyenc = NULL;
|
||||||
PyObject *pybenc = NULL;
|
|
||||||
char *rv = NULL;
|
char *rv = NULL;
|
||||||
|
|
||||||
/* Find the Py codec name from the PG encoding */
|
/* Find the Py codec name from the PG encoding */
|
||||||
|
@ -250,11 +249,12 @@ conn_encoding_to_codec(const char *enc)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Convert the codec in a bytes string to extract the c string. */
|
/* Convert the codec in a bytes string to extract the c string. */
|
||||||
if (!(pybenc = psycopg_ensure_bytes(pyenc))) {
|
Py_INCREF(pyenc);
|
||||||
|
if (!(pyenc = psycopg_ensure_bytes(pyenc))) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (-1 == Bytes_AsStringAndSize(pybenc, &tmp, &size)) {
|
if (-1 == Bytes_AsStringAndSize(pyenc, &tmp, &size)) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,8 +262,7 @@ conn_encoding_to_codec(const char *enc)
|
||||||
rv = psycopg_strdup(tmp, size);
|
rv = psycopg_strdup(tmp, size);
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
/* pyenc is borrowed: no decref. */
|
Py_XDECREF(pyenc);
|
||||||
Py_XDECREF(pybenc);
|
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -430,17 +430,17 @@ static PyObject *
|
||||||
typecast_repr(PyObject *self)
|
typecast_repr(PyObject *self)
|
||||||
{
|
{
|
||||||
PyObject *name = ((typecastObject *)self)->name;
|
PyObject *name = ((typecastObject *)self)->name;
|
||||||
PyObject *bname;
|
|
||||||
PyObject *rv;
|
PyObject *rv;
|
||||||
|
|
||||||
if (!(bname = psycopg_ensure_bytes(name))) {
|
Py_INCREF(name);
|
||||||
|
if (!(name = psycopg_ensure_bytes(name))) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = PyString_FromFormat("<%s '%s' at %p>",
|
rv = PyString_FromFormat("<%s '%s' at %p>",
|
||||||
Py_TYPE(self)->tp_name, PyBytes_AS_STRING(name), self);
|
Py_TYPE(self)->tp_name, PyBytes_AS_STRING(name), self);
|
||||||
|
|
||||||
Py_DECREF(bname);
|
Py_DECREF(name);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -96,23 +96,30 @@ psycopg_strdup(const char *from, Py_ssize_t len)
|
||||||
*
|
*
|
||||||
* Useful when a char * is required out of it.
|
* Useful when a char * is required out of it.
|
||||||
*
|
*
|
||||||
* Return a new reference. NULL on error.
|
* The function is ref neutral: steals a ref from obj and adds one to the
|
||||||
|
* return value. This also means that you shouldn't call the function on a
|
||||||
|
* borrowed ref, if having the object unallocated is not what you want.
|
||||||
|
*
|
||||||
|
* It is safe to call the function on NULL.
|
||||||
*/
|
*/
|
||||||
PyObject *
|
PyObject *
|
||||||
psycopg_ensure_bytes(PyObject *obj)
|
psycopg_ensure_bytes(PyObject *obj)
|
||||||
{
|
{
|
||||||
PyObject *rv = NULL;
|
PyObject *rv = NULL;
|
||||||
|
if (!obj) { return NULL; }
|
||||||
|
|
||||||
if (PyUnicode_CheckExact(obj)) {
|
if (PyUnicode_CheckExact(obj)) {
|
||||||
rv = PyUnicode_AsUTF8String(obj);
|
rv = PyUnicode_AsUTF8String(obj);
|
||||||
|
Py_DECREF(obj);
|
||||||
}
|
}
|
||||||
else if (Bytes_CheckExact(obj)) {
|
else if (Bytes_CheckExact(obj)) {
|
||||||
Py_INCREF(obj);
|
|
||||||
rv = obj;
|
rv = obj;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_Format(PyExc_TypeError, "I'm not into ensuring %s as bytes",
|
PyErr_Format(PyExc_TypeError,
|
||||||
obj ? Py_TYPE(obj)->tp_name : "NULL");
|
"Expected bytes or unicode string, got %s instead",
|
||||||
|
Py_TYPE(obj)->tp_name);
|
||||||
|
Py_DECREF(obj); /* steal the ref anyway */
|
||||||
}
|
}
|
||||||
|
|
||||||
return rv;
|
return rv;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user