Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 66abb0e

Browse filesBrowse files
author
adustman
committed
A lot more Python 3 fixes for _mysql. It still ain't fixed or broken yet (for Python <3)
1 parent 9737a4c commit 66abb0e
Copy full SHA for 66abb0e

File tree

Expand file treeCollapse file tree

1 file changed

+116
-3
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+116
-3
lines changed

‎MySQLdb/_mysql.c

Copy file name to clipboardExpand all lines: MySQLdb/_mysql.c
+116-3Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
2626
PERFORMANCE OF THIS SOFTWARE.
2727
*/
2828

29+
#include "Python.h"
2930
#if PY_MAJOR_VERSION >= 3
3031
#define IS_PY3K
32+
#define PyInt_FromLong(n) PyLong_FromLong(n)
33+
#define PyInt_Check(n) PyLong_Check(n)
34+
#define PyInt_AS_LONG(n) PyLong_AS_LONG(n)
3135
#endif
3236
#if PY_VERSION_HEX > 0x02060000
3337
#include "bytesobject.h"
34-
#include "intobject.h"
3538
#endif
3639
#include "pymemcompat.h"
3740
#include "structmember.h"
@@ -121,7 +124,11 @@ _mysql_Exception(_mysql_ConnectionObject *c)
121124
if (!_mysql_server_init_done) {
122125
e = _mysql_InternalError;
123126
PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L));
127+
#ifdef IS_PY3K
128+
PyTuple_SET_ITEM(t, 1, PyUnicode_FromString("server not initialized"));
129+
#else
124130
PyTuple_SET_ITEM(t, 1, PyString_FromString("server not initialized"));
131+
#endif
125132
PyErr_SetObject(e, t);
126133
Py_DECREF(t);
127134
return NULL;
@@ -131,7 +138,11 @@ _mysql_Exception(_mysql_ConnectionObject *c)
131138
e = _mysql_InterfaceError;
132139
else if (merr > CR_MAX_ERROR) {
133140
PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L));
141+
#ifdef IS_PY3K
142+
PyTuple_SET_ITEM(t, 1, PyUnicode_FromString("error totally whack"));
143+
#else
134144
PyTuple_SET_ITEM(t, 1, PyString_FromString("error totally whack"));
145+
#endif
135146
PyErr_SetObject(_mysql_InterfaceError, t);
136147
Py_DECREF(t);
137148
return NULL;
@@ -219,7 +230,11 @@ _mysql_Exception(_mysql_ConnectionObject *c)
219230
break;
220231
}
221232
PyTuple_SET_ITEM(t, 0, PyInt_FromLong((long)merr));
233+
#ifdef IS_PY3K
234+
PyTuple_SET_ITEM(t, 1, PyUnicode_FromString(mysql_error(&(c->connection))));
235+
#else
222236
PyTuple_SET_ITEM(t, 1, PyString_FromString(mysql_error(&(c->connection))));
237+
#endif
223238
PyErr_SetObject(e, t);
224239
Py_DECREF(t);
225240
return NULL;
@@ -268,7 +283,12 @@ static PyObject *_mysql_server_init(
268283
cmd_args_c = (char **) PyMem_Malloc(cmd_argc*sizeof(char *));
269284
for (i=0; i< cmd_argc; i++) {
270285
item = PySequence_GetItem(cmd_args, i);
286+
#ifdef IS_PY3K
287+
s = PyUnicode_AS_DATA(item);
288+
#else
271289
s = PyString_AsString(item);
290+
#endif
291+
272292
Py_DECREF(item);
273293
if (!s) {
274294
PyErr_SetString(PyExc_TypeError,
@@ -293,7 +313,11 @@ static PyObject *_mysql_server_init(
293313
groups_c = (char **) PyMem_Malloc((1+groupc)*sizeof(char *));
294314
for (i=0; i< groupc; i++) {
295315
item = PySequence_GetItem(groups, i);
316+
#ifdef IS_PY3K
317+
s = PyUnicode_AS_DATA(item);
318+
#else
296319
s = PyString_AsString(item);
320+
#endif
297321
Py_DECREF(item);
298322
if (!s) {
299323
PyErr_SetString(PyExc_TypeError,
@@ -527,9 +551,15 @@ _mysql_ConnectionObject_Initialize(
527551
))
528552
return -1;
529553

554+
#ifdef IS_PY3K
555+
#define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\
556+
if(t){d=PyUnicode_AS_DATA(t);Py_DECREF(t);}\
557+
PyErr_Clear();}
558+
#else
530559
#define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\
531560
if(t){d=PyString_AsString(t);Py_DECREF(t);}\
532561
PyErr_Clear();}
562+
#endif
533563

534564
if (ssl) {
535565
#if HAVE_OPENSSL
@@ -919,8 +949,12 @@ _mysql_ConnectionObject_sqlstate(
919949
PyObject *args)
920950
{
921951
if (!PyArg_ParseTuple(args, "")) return NULL;
952+
#ifdef IS_PY3K
953+
return PyUnicode_FromString(mysql_sqlstate(&(self->connection)));
954+
#else
922955
return PyString_FromString(mysql_sqlstate(&(self->connection)));
923-
}
956+
#endif
957+
}
924958

925959
static char _mysql_ConnectionObject_warning_count__doc__[] =
926960
"Returns the number of warnings generated during execution\n\
@@ -968,7 +1002,11 @@ _mysql_ConnectionObject_error(
9681002
{
9691003
if (!PyArg_ParseTuple(args, "")) return NULL;
9701004
check_connection(self);
1005+
#ifdef IS_PY3K
1006+
return PyUnicode_FromString(mysql_error(&(self->connection)));
1007+
#else
9711008
return PyString_FromString(mysql_error(&(self->connection)));
1009+
#endif
9721010
}
9731011

9741012
static char _mysql_escape_string__doc__[] =
@@ -988,9 +1026,17 @@ _mysql_escape_string(
9881026
char *in, *out;
9891027
int len, size;
9901028
if (!PyArg_ParseTuple(args, "s#:escape_string", &in, &size)) return NULL;
1029+
#ifdef IS_PY3K
1030+
str = PyUnicode_FromStringAndSize((char *) NULL, size*2+1);
1031+
#else
9911032
str = PyString_FromStringAndSize((char *) NULL, size*2+1);
1033+
#endif
9921034
if (!str) return PyErr_NoMemory();
993-
out = PyString_AS_STRING(str);
1035+
#ifdef IS_PY3K
1036+
out = PyUnicode_AS_DATA(str);
1037+
#else
1038+
out = PyString_AS_STRING(str);
1039+
#endif
9941040
#if MYSQL_VERSION_ID < 32321
9951041
len = mysql_escape_string(out, in, size);
9961042
#else
@@ -1000,7 +1046,11 @@ _mysql_escape_string(
10001046
else
10011047
len = mysql_escape_string(out, in, size);
10021048
#endif
1049+
#ifdef IS_PY3K
1050+
if (PyUnicode_Resize(&str, len) < 0) return NULL;
1051+
#else
10031052
if (_PyString_Resize(&str, len) < 0) return NULL;
1053+
#endif
10041054
return (str);
10051055
}
10061056

@@ -1025,11 +1075,19 @@ _mysql_string_literal(
10251075
if (!PyArg_ParseTuple(args, "O|O:string_literal", &o, &d)) return NULL;
10261076
s = PyObject_Str(o);
10271077
if (!s) return NULL;
1078+
#ifdef IS_PY3K
1079+
in = PyUnicode_AS_DATA(s);
1080+
size = PyUnicode_GetSize(s);
1081+
str = PyUnicode_FromStringAndSize((char *) NULL, size*2+3);
1082+
if (!str) return PyErr_NoMemory();
1083+
out = PyUnicode_AS_DATA(str);
1084+
#else
10281085
in = PyString_AsString(s);
10291086
size = PyString_GET_SIZE(s);
10301087
str = PyString_FromStringAndSize((char *) NULL, size*2+3);
10311088
if (!str) return PyErr_NoMemory();
10321089
out = PyString_AS_STRING(str);
1090+
#endif
10331091
#if MYSQL_VERSION_ID < 32321
10341092
len = mysql_escape_string(out+1, in, size);
10351093
#else
@@ -1040,7 +1098,11 @@ _mysql_string_literal(
10401098
len = mysql_escape_string(out+1, in, size);
10411099
#endif
10421100
*out = *(out+len+1) = '\'';
1101+
#ifdef IS_PY3K
1102+
if (PyUnicode_Resize(&str, len+2) < 0) return NULL;
1103+
#else
10431104
if (_PyString_Resize(&str, len+2) < 0) return NULL;
1105+
#endif
10441106
Py_DECREF(s);
10451107
return (str);
10461108
}
@@ -1060,7 +1122,11 @@ _escape_item(
10601122
if (!itemconv) {
10611123
PyErr_Clear();
10621124
itemconv = PyObject_GetItem(d,
1125+
#ifdef IS_PY3K
1126+
(PyObject *) &PyUnicode_Type);
1127+
#else
10631128
(PyObject *) &PyString_Type);
1129+
#endif
10641130
}
10651131
if (!itemconv) {
10661132
PyErr_SetString(PyExc_TypeError,
@@ -1247,8 +1313,13 @@ _mysql_field_to_python(
12471313
rowitem,
12481314
(int)length);
12491315
else
1316+
#ifdef IS_PY3K
1317+
v = PyUnicode_FromStringAndSize(rowitem,
1318+
(int)length);
1319+
#else
12501320
v = PyString_FromStringAndSize(rowitem,
12511321
(int)length);
1322+
#endif
12521323
if (!v)
12531324
return NULL;
12541325
} else {
@@ -1529,7 +1600,11 @@ _mysql_ConnectionObject_character_set_name(
15291600
#else
15301601
s = "latin1";
15311602
#endif
1603+
#ifdef IS_PY3K
1604+
return PyUnicode_FromString(s);
1605+
#else
15321606
return PyString_FromString(s);
1607+
#endif
15331608
}
15341609

15351610
#if MYSQL_VERSION_ID >= 50007
@@ -1590,6 +1665,18 @@ _mysql_ConnectionObject_get_character_set_info(
15901665
check_connection(self);
15911666
mysql_get_character_set_info(&(self->connection), &cs);
15921667
if (!(result = PyDict_New())) return NULL;
1668+
#ifdef IS_PY3K
1669+
if (cs.csname)
1670+
PyDict_SetItemString(result, "name", PyUnicode_FromString(cs.csname));
1671+
if (cs.name)
1672+
PyDict_SetItemString(result, "collation", PyUnicode_FromString(cs.name));
1673+
if (cs.comment)
1674+
PyDict_SetItemString(result, "comment", PyUnicode_FromString(cs.comment));
1675+
if (cs.dir)
1676+
PyDict_SetItemString(result, "dir", PyUnicode_FromString(cs.dir));
1677+
PyDict_SetItemString(result, "mbminlen", PyInt_FromLong(cs.mbminlen));
1678+
PyDict_SetItemString(result, "mbmaxlen", PyInt_FromLong(cs.mbmaxlen));
1679+
#else
15931680
if (cs.csname)
15941681
PyDict_SetItemString(result, "name", PyString_FromString(cs.csname));
15951682
if (cs.name)
@@ -1600,6 +1687,7 @@ _mysql_ConnectionObject_get_character_set_info(
16001687
PyDict_SetItemString(result, "dir", PyString_FromString(cs.dir));
16011688
PyDict_SetItemString(result, "mbminlen", PyInt_FromLong(cs.mbminlen));
16021689
PyDict_SetItemString(result, "mbmaxlen", PyInt_FromLong(cs.mbmaxlen));
1690+
#endif
16031691
return result;
16041692
}
16051693
#endif
@@ -1614,7 +1702,11 @@ _mysql_get_client_info(
16141702
{
16151703
if (!PyArg_ParseTuple(args, "")) return NULL;
16161704
check_server_init(NULL);
1705+
#ifdef IS_PY3K
1706+
return PyUnicode_FromString(mysql_get_client_info());
1707+
#else
16171708
return PyString_FromString(mysql_get_client_info());
1709+
#endif
16181710
}
16191711

16201712
static char _mysql_ConnectionObject_get_host_info__doc__[] =
@@ -1629,7 +1721,11 @@ _mysql_ConnectionObject_get_host_info(
16291721
{
16301722
if (!PyArg_ParseTuple(args, "")) return NULL;
16311723
check_connection(self);
1724+
#ifdef IS_PY3K
1725+
return PyUnicode_FromString(mysql_get_host_info(&(self->connection)));
1726+
#else
16321727
return PyString_FromString(mysql_get_host_info(&(self->connection)));
1728+
#endif
16331729
}
16341730

16351731
static char _mysql_ConnectionObject_get_proto_info__doc__[] =
@@ -1659,7 +1755,11 @@ _mysql_ConnectionObject_get_server_info(
16591755
{
16601756
if (!PyArg_ParseTuple(args, "")) return NULL;
16611757
check_connection(self);
1758+
#ifdef IS_PY3K
1759+
return PyUnicode_FromString(mysql_get_server_info(&(self->connection)));
1760+
#else
16621761
return PyString_FromString(mysql_get_server_info(&(self->connection)));
1762+
#endif
16631763
}
16641764

16651765
static char _mysql_ConnectionObject_info__doc__[] =
@@ -1677,7 +1777,11 @@ _mysql_ConnectionObject_info(
16771777
if (!PyArg_ParseTuple(args, "")) return NULL;
16781778
check_connection(self);
16791779
s = mysql_info(&(self->connection));
1780+
#ifdef IS_PY3K
1781+
if (s) return PyUnicode_FromString(s);
1782+
#else
16801783
if (s) return PyString_FromString(s);
1784+
#endif
16811785
Py_INCREF(Py_None);
16821786
return Py_None;
16831787
}
@@ -1920,7 +2024,12 @@ _mysql_ConnectionObject_stat(
19202024
s = mysql_stat(&(self->connection));
19212025
Py_END_ALLOW_THREADS
19222026
if (!s) return _mysql_Exception(self);
2027+
#ifdef IS_PY3K
2028+
return PyUnicode_FromString(s);
2029+
#else
19232030
return PyString_FromString(s);
2031+
#endif
2032+
19242033
}
19252034

19262035
static char _mysql_ConnectionObject_store_result__doc__[] =
@@ -2046,7 +2155,11 @@ _mysql_ConnectionObject_repr(
20462155
else
20472156
sprintf(buf, "<_mysql.connection closed at %lx>",
20482157
(long)self);
2158+
#ifdef IS_PY3K
2159+
return PyUnicode_FromString(buf);
2160+
#else
20492161
return PyString_FromString(buf);
2162+
#endif
20502163
}
20512164

20522165
static char _mysql_ResultObject_data_seek__doc__[] =

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.