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 */
|
||||
|
||||
static PyObject *
|
||||
pydatetime_str(pydatetimeObject *self)
|
||||
_pydatetime_string_date_time(pydatetimeObject *self)
|
||||
{
|
||||
PyObject *res = NULL;
|
||||
PyObject *iso;
|
||||
if (self->type <= PSYCO_DATETIME_TIMESTAMP) {
|
||||
PyObject *rv = NULL;
|
||||
PyObject *iso = NULL;
|
||||
PyObject *biso = NULL;
|
||||
PyObject *tz;
|
||||
|
||||
/* Select the right PG type to cast into. */
|
||||
|
@ -73,31 +73,35 @@ pydatetime_str(pydatetimeObject *self)
|
|||
break;
|
||||
case PSYCO_DATETIME_TIMESTAMP:
|
||||
tz = PyObject_GetAttrString(self->wrapped, "tzinfo");
|
||||
if (!tz) { return NULL; }
|
||||
if (!tz) { goto error; }
|
||||
fmt = (tz == Py_None) ? "'%s'::timestamp" : "'%s'::timestamptz";
|
||||
Py_DECREF(tz);
|
||||
break;
|
||||
}
|
||||
|
||||
iso = PyObject_CallMethod(self->wrapped, "isoformat", NULL);
|
||||
if (iso) {
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
{
|
||||
PyObject *biso;
|
||||
if (!(biso = PyUnicode_AsEncodedString(iso, "ascii", NULL))) {
|
||||
if (!(iso = PyObject_CallMethod(self->wrapped, "isoformat", NULL))) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!(biso = psycopg_ensure_bytes(iso))) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
rv = Bytes_FromFormat(fmt, Bytes_AsString(biso));
|
||||
|
||||
Py_DECREF(biso);
|
||||
Py_DECREF(iso);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(iso);
|
||||
iso = biso;
|
||||
}
|
||||
#endif
|
||||
res = Bytes_FromFormat(fmt, Bytes_AsString(iso));
|
||||
Py_DECREF(iso);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
else {
|
||||
return rv;
|
||||
|
||||
error:
|
||||
Py_XDECREF(biso);
|
||||
Py_XDECREF(iso);
|
||||
return rv;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
_pydatetime_string_delta(pydatetimeObject *self)
|
||||
{
|
||||
PyDateTime_Delta *obj = (PyDateTime_Delta*)self->wrapped;
|
||||
|
||||
char buffer[8];
|
||||
|
@ -112,6 +116,16 @@ pydatetime_str(pydatetimeObject *self)
|
|||
|
||||
return Bytes_FromFormat("'%d days %d.%s seconds'::interval",
|
||||
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;
|
||||
}
|
||||
|
||||
/* 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 (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);
|
||||
/* Convert the codec in a bytes string to extract the c string. */
|
||||
if (!(pybenc = psycopg_ensure_bytes(pyenc))) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user