mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-02-07 21:00:33 +03:00
Use psycopg_ensure_bytes() to unify Py2/3 code paths.
This commit is contained in:
parent
89e4d4c7bb
commit
014b6a6d5b
|
@ -55,11 +55,11 @@ psyco_adapter_datetime_init(void)
|
||||||
/* datetime_str, datetime_getquoted - return result of quoting */
|
/* datetime_str, datetime_getquoted - return result of quoting */
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
pydatetime_str(pydatetimeObject *self)
|
_pydatetime_string_date_time(pydatetimeObject *self)
|
||||||
{
|
{
|
||||||
PyObject *res = NULL;
|
PyObject *rv = NULL;
|
||||||
PyObject *iso;
|
PyObject *iso = NULL;
|
||||||
if (self->type <= PSYCO_DATETIME_TIMESTAMP) {
|
PyObject *biso = NULL;
|
||||||
PyObject *tz;
|
PyObject *tz;
|
||||||
|
|
||||||
/* Select the right PG type to cast into. */
|
/* Select the right PG type to cast into. */
|
||||||
|
@ -73,31 +73,35 @@ pydatetime_str(pydatetimeObject *self)
|
||||||
break;
|
break;
|
||||||
case PSYCO_DATETIME_TIMESTAMP:
|
case PSYCO_DATETIME_TIMESTAMP:
|
||||||
tz = PyObject_GetAttrString(self->wrapped, "tzinfo");
|
tz = PyObject_GetAttrString(self->wrapped, "tzinfo");
|
||||||
if (!tz) { return NULL; }
|
if (!tz) { goto error; }
|
||||||
fmt = (tz == Py_None) ? "'%s'::timestamp" : "'%s'::timestamptz";
|
fmt = (tz == Py_None) ? "'%s'::timestamp" : "'%s'::timestamptz";
|
||||||
Py_DECREF(tz);
|
Py_DECREF(tz);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
iso = PyObject_CallMethod(self->wrapped, "isoformat", NULL);
|
if (!(iso = PyObject_CallMethod(self->wrapped, "isoformat", NULL))) {
|
||||||
if (iso) {
|
goto error;
|
||||||
#if PY_MAJOR_VERSION > 2
|
}
|
||||||
{
|
|
||||||
PyObject *biso;
|
if (!(biso = psycopg_ensure_bytes(iso))) {
|
||||||
if (!(biso = PyUnicode_AsEncodedString(iso, "ascii", NULL))) {
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
rv = Bytes_FromFormat(fmt, Bytes_AsString(biso));
|
||||||
|
|
||||||
|
Py_DECREF(biso);
|
||||||
Py_DECREF(iso);
|
Py_DECREF(iso);
|
||||||
return NULL;
|
return rv;
|
||||||
}
|
|
||||||
Py_DECREF(iso);
|
error:
|
||||||
iso = biso;
|
Py_XDECREF(biso);
|
||||||
}
|
Py_XDECREF(iso);
|
||||||
#endif
|
return rv;
|
||||||
res = Bytes_FromFormat(fmt, Bytes_AsString(iso));
|
}
|
||||||
Py_DECREF(iso);
|
|
||||||
}
|
static PyObject *
|
||||||
return res;
|
_pydatetime_string_delta(pydatetimeObject *self)
|
||||||
}
|
{
|
||||||
else {
|
|
||||||
PyDateTime_Delta *obj = (PyDateTime_Delta*)self->wrapped;
|
PyDateTime_Delta *obj = (PyDateTime_Delta*)self->wrapped;
|
||||||
|
|
||||||
char buffer[8];
|
char buffer[8];
|
||||||
|
@ -112,6 +116,16 @@ pydatetime_str(pydatetimeObject *self)
|
||||||
|
|
||||||
return Bytes_FromFormat("'%d days %d.%s seconds'::interval",
|
return Bytes_FromFormat("'%d days %d.%s seconds'::interval",
|
||||||
obj->days, obj->seconds, buffer);
|
obj->days, obj->seconds, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
pydatetime_str(pydatetimeObject *self)
|
||||||
|
{
|
||||||
|
if (self->type <= PSYCO_DATETIME_TIMESTAMP) {
|
||||||
|
return _pydatetime_string_date_time(self);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return _pydatetime_string_delta(self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -235,20 +235,8 @@ conn_encoding_to_codec(const char *enc)
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Convert the codec in a bytes string to extract the c string.
|
/* Convert the codec in a bytes string to extract the c string. */
|
||||||
* At the end of the block we have pybenc with a new ref. */
|
if (!(pybenc = psycopg_ensure_bytes(pyenc))) {
|
||||||
if (PyUnicode_Check(pyenc)) {
|
|
||||||
if (!(pybenc = PyUnicode_AsEncodedString(pyenc, "ascii", NULL))) {
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (Bytes_Check(pyenc)) {
|
|
||||||
Py_INCREF(pyenc);
|
|
||||||
pybenc = pyenc;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
PyErr_Format(PyExc_TypeError, "bad type for encoding: %s",
|
|
||||||
Py_TYPE(pyenc)->tp_name);
|
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user