Skip to content

Commit 3214c23

Browse files
committed
Fixed adaptation in several adapters.
The getquoted methods always return bytes. The str() convert this representation to string on the fly.
1 parent 2e22eef commit 3214c23

File tree

9 files changed

+69
-41
lines changed

9 files changed

+69
-41
lines changed

psycopg/adapter_asis.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,31 @@
3535
/** the AsIs object **/
3636

3737
static PyObject *
38-
asis_str(asisObject *self)
38+
asis_getquoted(asisObject *self, PyObject *args)
3939
{
40+
PyObject *rv;
4041
if (self->wrapped == Py_None) {
41-
return Bytes_FromString("NULL");
42+
rv = Bytes_FromString("NULL");
4243
}
4344
else {
44-
return PyObject_Str(self->wrapped);
45+
rv = PyObject_Str(self->wrapped);
46+
#if PY_MAJOR_VERSION > 2
47+
/* unicode to bytes in Py3 */
48+
if (rv) {
49+
PyObject *tmp = PyUnicode_AsUTF8String(rv);
50+
Py_DECREF(rv);
51+
rv = tmp;
52+
}
53+
#endif
4554
}
55+
56+
return rv;
4657
}
4758

4859
static PyObject *
49-
asis_getquoted(asisObject *self, PyObject *args)
60+
asis_str(asisObject *self)
5061
{
51-
return asis_str(self);
62+
return psycopg_ensure_text(asis_getquoted(self, NULL));
5263
}
5364

5465
static PyObject *

psycopg/adapter_binary.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,23 @@ binary_quote(binaryObject *self)
9898
}
9999

100100
/* binary_str, binary_getquoted - return result of quoting */
101-
/* XXX what is the point of this method? */
101+
102102
static PyObject *
103-
binary_str(binaryObject *self)
103+
binary_getquoted(binaryObject *self, PyObject *args)
104104
{
105105
if (self->buffer == NULL) {
106106
if (!(binary_quote(self))) {
107107
return NULL;
108108
}
109109
}
110-
#if PY_MAJOR_VERSION < 3
111110
Py_INCREF(self->buffer);
112111
return self->buffer;
113-
#else
114-
return PyUnicode_FromEncodedObject(self->buffer, "ascii", "replace");
115-
#endif
116112
}
117113

118114
static PyObject *
119-
binary_getquoted(binaryObject *self, PyObject *args)
115+
binary_str(binaryObject *self)
120116
{
121-
return binary_str(self);
117+
return psycopg_ensure_text(binary_getquoted(self, NULL));
122118
}
123119

124120
static PyObject *

psycopg/adapter_datetime.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ _pydatetime_string_delta(pydatetimeObject *self)
119119
}
120120

121121
static PyObject *
122-
pydatetime_str(pydatetimeObject *self)
122+
pydatetime_getquoted(pydatetimeObject *self, PyObject *args)
123123
{
124124
if (self->type <= PSYCO_DATETIME_TIMESTAMP) {
125125
return _pydatetime_string_date_time(self);
@@ -130,9 +130,9 @@ pydatetime_str(pydatetimeObject *self)
130130
}
131131

132132
static PyObject *
133-
pydatetime_getquoted(pydatetimeObject *self, PyObject *args)
133+
pydatetime_str(pydatetimeObject *self)
134134
{
135-
return pydatetime_str(self);
135+
return psycopg_ensure_text(pydatetime_getquoted(self, NULL));
136136
}
137137

138138
static PyObject *

psycopg/adapter_list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ list_quote(listObject *self)
8383
static PyObject *
8484
list_str(listObject *self)
8585
{
86-
return list_quote(self);
86+
return psycopg_ensure_text(list_quote(self));
8787
}
8888

8989
static PyObject *

psycopg/adapter_pboolean.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,29 @@
3535
/** the Boolean object **/
3636

3737
static PyObject *
38-
pboolean_str(pbooleanObject *self)
38+
pboolean_getquoted(pbooleanObject *self, PyObject *args)
3939
{
4040
#ifdef PSYCOPG_NEW_BOOLEAN
4141
if (PyObject_IsTrue(self->wrapped)) {
42-
return Text_FromUTF8("true");
42+
return Bytes_FromString("true");
4343
}
4444
else {
45-
return Text_FromUTF8("false");
45+
return Bytes_FromString("false");
4646
}
4747
#else
4848
if (PyObject_IsTrue(self->wrapped)) {
49-
return Text_FromUTF8("'t'");
49+
return Bytes_FromString("'t'");
5050
}
5151
else {
52-
return Text_FromUTF8("'f'");
52+
return Bytes_FromString("'f'");
5353
}
5454
#endif
5555
}
5656

5757
static PyObject *
58-
pboolean_getquoted(pbooleanObject *self, PyObject *args)
58+
pboolean_str(pbooleanObject *self)
5959
{
60-
return pboolean_str(self);
60+
return psycopg_ensure_text(pboolean_getquoted(self, NULL));
6161
}
6262

6363
static PyObject *

psycopg/adapter_pdecimal.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/** the Decimal object **/
3737

3838
static PyObject *
39-
pdecimal_str(pdecimalObject *self)
39+
pdecimal_getquoted(pdecimalObject *self, PyObject *args)
4040
{
4141
PyObject *check, *res = NULL;
4242
check = PyObject_CallMethod(self->wrapped, "is_finite", NULL);
@@ -45,7 +45,7 @@ pdecimal_str(pdecimalObject *self)
4545
goto end;
4646
}
4747
else if (check) {
48-
res = Text_FromUTF8("'NaN'::numeric");
48+
res = Bytes_FromString("'NaN'::numeric");
4949
goto end;
5050
}
5151

@@ -57,7 +57,7 @@ pdecimal_str(pdecimalObject *self)
5757
goto end;
5858
}
5959
if (PyObject_IsTrue(check)) {
60-
res = Text_FromUTF8("'NaN'::numeric");
60+
res = Bytes_FromString("'NaN'::numeric");
6161
goto end;
6262
}
6363

@@ -66,21 +66,29 @@ pdecimal_str(pdecimalObject *self)
6666
goto end;
6767
}
6868
if (PyObject_IsTrue(check)) {
69-
res = Text_FromUTF8("'NaN'::numeric");
69+
res = Bytes_FromString("'NaN'::numeric");
7070
goto end;
7171
}
7272

7373
res = PyObject_Str(self->wrapped);
74+
#if PY_MAJOR_VERSION > 2
75+
/* unicode to bytes in Py3 */
76+
if (res) {
77+
PyObject *tmp = PyUnicode_AsUTF8String(res);
78+
Py_DECREF(res);
79+
res = tmp;
80+
}
81+
#endif
7482

7583
end:
7684
Py_XDECREF(check);
7785
return res;
7886
}
7987

8088
static PyObject *
81-
pdecimal_getquoted(pdecimalObject *self, PyObject *args)
89+
pdecimal_str(pdecimalObject *self)
8290
{
83-
return pdecimal_str(self);
91+
return psycopg_ensure_text(pdecimal_getquoted(self, NULL));
8492
}
8593

8694
static PyObject *

psycopg/adapter_pfloat.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,34 @@
3636
/** the Float object **/
3737

3838
static PyObject *
39-
pfloat_str(pfloatObject *self)
39+
pfloat_getquoted(pfloatObject *self, PyObject *args)
4040
{
41+
PyObject *rv;
4142
double n = PyFloat_AsDouble(self->wrapped);
4243
if (isnan(n))
43-
return Text_FromUTF8("'NaN'::float");
44+
rv = Bytes_FromString("'NaN'::float");
4445
else if (isinf(n))
45-
return Text_FromUTF8("'Infinity'::float");
46-
else
47-
return PyObject_Repr(self->wrapped);
46+
rv = Bytes_FromString("'Infinity'::float");
47+
else {
48+
rv = PyObject_Repr(self->wrapped);
49+
50+
#if PY_MAJOR_VERSION > 2
51+
/* unicode to bytes in Py3 */
52+
if (rv) {
53+
PyObject *tmp = PyUnicode_AsUTF8String(rv);
54+
Py_DECREF(rv);
55+
rv = tmp;
56+
}
57+
#endif
58+
}
59+
60+
return rv;
4861
}
4962

5063
static PyObject *
51-
pfloat_getquoted(pfloatObject *self, PyObject *args)
64+
pfloat_str(pfloatObject *self)
5265
{
53-
return pfloat_str(self);
66+
return psycopg_ensure_text(pfloat_getquoted(self, NULL));
5467
}
5568

5669
static PyObject *

psycopg/adapter_qstring.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ qstring_quote(qstringObject *self)
106106
/* qstring_str, qstring_getquoted - return result of quoting */
107107

108108
static PyObject *
109-
qstring_str(qstringObject *self)
109+
qstring_getquoted(qstringObject *self, PyObject *args)
110110
{
111111
if (self->buffer == NULL) {
112112
qstring_quote(self);
@@ -116,9 +116,9 @@ qstring_str(qstringObject *self)
116116
}
117117

118118
static PyObject *
119-
qstring_getquoted(qstringObject *self, PyObject *args)
119+
qstring_str(qstringObject *self)
120120
{
121-
return qstring_str(self);
121+
return psycopg_ensure_text(qstring_getquoted(self, NULL));
122122
}
123123

124124
static PyObject *

psycopg/microprotocols.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
138138
/* None is always adapted to NULL */
139139

140140
if (obj == Py_None)
141-
return Text_FromUTF8("NULL");
141+
return Bytes_FromString("NULL");
142142

143143
Dprintf("microprotocols_adapt: trying to adapt %s",
144144
Py_TYPE(obj)->tp_name);

0 commit comments

Comments
 (0)