@@ -26,12 +26,15 @@ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
26
26
PERFORMANCE OF THIS SOFTWARE.
27
27
*/
28
28
29
+ #include "Python.h"
29
30
#if PY_MAJOR_VERSION >= 3
30
31
#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)
31
35
#endif
32
36
#if PY_VERSION_HEX > 0x02060000
33
37
#include "bytesobject.h"
34
- #include "intobject.h"
35
38
#endif
36
39
#include "pymemcompat.h"
37
40
#include "structmember.h"
@@ -121,7 +124,11 @@ _mysql_Exception(_mysql_ConnectionObject *c)
121
124
if (!_mysql_server_init_done ) {
122
125
e = _mysql_InternalError ;
123
126
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
124
130
PyTuple_SET_ITEM (t , 1 , PyString_FromString ("server not initialized" ));
131
+ #endif
125
132
PyErr_SetObject (e , t );
126
133
Py_DECREF (t );
127
134
return NULL ;
@@ -131,7 +138,11 @@ _mysql_Exception(_mysql_ConnectionObject *c)
131
138
e = _mysql_InterfaceError ;
132
139
else if (merr > CR_MAX_ERROR ) {
133
140
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
134
144
PyTuple_SET_ITEM (t , 1 , PyString_FromString ("error totally whack" ));
145
+ #endif
135
146
PyErr_SetObject (_mysql_InterfaceError , t );
136
147
Py_DECREF (t );
137
148
return NULL ;
@@ -219,7 +230,11 @@ _mysql_Exception(_mysql_ConnectionObject *c)
219
230
break ;
220
231
}
221
232
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
222
236
PyTuple_SET_ITEM (t , 1 , PyString_FromString (mysql_error (& (c -> connection ))));
237
+ #endif
223
238
PyErr_SetObject (e , t );
224
239
Py_DECREF (t );
225
240
return NULL ;
@@ -268,7 +283,12 @@ static PyObject *_mysql_server_init(
268
283
cmd_args_c = (char * * ) PyMem_Malloc (cmd_argc * sizeof (char * ));
269
284
for (i = 0 ; i < cmd_argc ; i ++ ) {
270
285
item = PySequence_GetItem (cmd_args , i );
286
+ #ifdef IS_PY3K
287
+ s = PyUnicode_AS_DATA (item );
288
+ #else
271
289
s = PyString_AsString (item );
290
+ #endif
291
+
272
292
Py_DECREF (item );
273
293
if (!s ) {
274
294
PyErr_SetString (PyExc_TypeError ,
@@ -293,7 +313,11 @@ static PyObject *_mysql_server_init(
293
313
groups_c = (char * * ) PyMem_Malloc ((1 + groupc )* sizeof (char * ));
294
314
for (i = 0 ; i < groupc ; i ++ ) {
295
315
item = PySequence_GetItem (groups , i );
316
+ #ifdef IS_PY3K
317
+ s = PyUnicode_AS_DATA (item );
318
+ #else
296
319
s = PyString_AsString (item );
320
+ #endif
297
321
Py_DECREF (item );
298
322
if (!s ) {
299
323
PyErr_SetString (PyExc_TypeError ,
@@ -527,9 +551,15 @@ _mysql_ConnectionObject_Initialize(
527
551
))
528
552
return -1 ;
529
553
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
530
559
#define _stringsuck (d ,t ,s ) {t=PyMapping_GetItemString(s,#d);\
531
560
if(t){d=PyString_AsString(t);Py_DECREF(t);}\
532
561
PyErr_Clear();}
562
+ #endif
533
563
534
564
if (ssl ) {
535
565
#if HAVE_OPENSSL
@@ -919,8 +949,12 @@ _mysql_ConnectionObject_sqlstate(
919
949
PyObject * args )
920
950
{
921
951
if (!PyArg_ParseTuple (args , "" )) return NULL ;
952
+ #ifdef IS_PY3K
953
+ return PyUnicode_FromString (mysql_sqlstate (& (self -> connection )));
954
+ #else
922
955
return PyString_FromString (mysql_sqlstate (& (self -> connection )));
923
- }
956
+ #endif
957
+ }
924
958
925
959
static char _mysql_ConnectionObject_warning_count__doc__ [] =
926
960
"Returns the number of warnings generated during execution\n\
@@ -968,7 +1002,11 @@ _mysql_ConnectionObject_error(
968
1002
{
969
1003
if (!PyArg_ParseTuple (args , "" )) return NULL ;
970
1004
check_connection (self );
1005
+ #ifdef IS_PY3K
1006
+ return PyUnicode_FromString (mysql_error (& (self -> connection )));
1007
+ #else
971
1008
return PyString_FromString (mysql_error (& (self -> connection )));
1009
+ #endif
972
1010
}
973
1011
974
1012
static char _mysql_escape_string__doc__ [] =
@@ -988,9 +1026,17 @@ _mysql_escape_string(
988
1026
char * in , * out ;
989
1027
int len , size ;
990
1028
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
991
1032
str = PyString_FromStringAndSize ((char * ) NULL , size * 2 + 1 );
1033
+ #endif
992
1034
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
994
1040
#if MYSQL_VERSION_ID < 32321
995
1041
len = mysql_escape_string (out , in , size );
996
1042
#else
@@ -1000,7 +1046,11 @@ _mysql_escape_string(
1000
1046
else
1001
1047
len = mysql_escape_string (out , in , size );
1002
1048
#endif
1049
+ #ifdef IS_PY3K
1050
+ if (PyUnicode_Resize (& str , len ) < 0 ) return NULL ;
1051
+ #else
1003
1052
if (_PyString_Resize (& str , len ) < 0 ) return NULL ;
1053
+ #endif
1004
1054
return (str );
1005
1055
}
1006
1056
@@ -1025,11 +1075,19 @@ _mysql_string_literal(
1025
1075
if (!PyArg_ParseTuple (args , "O|O:string_literal" , & o , & d )) return NULL ;
1026
1076
s = PyObject_Str (o );
1027
1077
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
1028
1085
in = PyString_AsString (s );
1029
1086
size = PyString_GET_SIZE (s );
1030
1087
str = PyString_FromStringAndSize ((char * ) NULL , size * 2 + 3 );
1031
1088
if (!str ) return PyErr_NoMemory ();
1032
1089
out = PyString_AS_STRING (str );
1090
+ #endif
1033
1091
#if MYSQL_VERSION_ID < 32321
1034
1092
len = mysql_escape_string (out + 1 , in , size );
1035
1093
#else
@@ -1040,7 +1098,11 @@ _mysql_string_literal(
1040
1098
len = mysql_escape_string (out + 1 , in , size );
1041
1099
#endif
1042
1100
* out = * (out + len + 1 ) = '\'' ;
1101
+ #ifdef IS_PY3K
1102
+ if (PyUnicode_Resize (& str , len + 2 ) < 0 ) return NULL ;
1103
+ #else
1043
1104
if (_PyString_Resize (& str , len + 2 ) < 0 ) return NULL ;
1105
+ #endif
1044
1106
Py_DECREF (s );
1045
1107
return (str );
1046
1108
}
@@ -1060,7 +1122,11 @@ _escape_item(
1060
1122
if (!itemconv ) {
1061
1123
PyErr_Clear ();
1062
1124
itemconv = PyObject_GetItem (d ,
1125
+ #ifdef IS_PY3K
1126
+ (PyObject * ) & PyUnicode_Type );
1127
+ #else
1063
1128
(PyObject * ) & PyString_Type );
1129
+ #endif
1064
1130
}
1065
1131
if (!itemconv ) {
1066
1132
PyErr_SetString (PyExc_TypeError ,
@@ -1247,8 +1313,13 @@ _mysql_field_to_python(
1247
1313
rowitem ,
1248
1314
(int )length );
1249
1315
else
1316
+ #ifdef IS_PY3K
1317
+ v = PyUnicode_FromStringAndSize (rowitem ,
1318
+ (int )length );
1319
+ #else
1250
1320
v = PyString_FromStringAndSize (rowitem ,
1251
1321
(int )length );
1322
+ #endif
1252
1323
if (!v )
1253
1324
return NULL ;
1254
1325
} else {
@@ -1529,7 +1600,11 @@ _mysql_ConnectionObject_character_set_name(
1529
1600
#else
1530
1601
s = "latin1" ;
1531
1602
#endif
1603
+ #ifdef IS_PY3K
1604
+ return PyUnicode_FromString (s );
1605
+ #else
1532
1606
return PyString_FromString (s );
1607
+ #endif
1533
1608
}
1534
1609
1535
1610
#if MYSQL_VERSION_ID >= 50007
@@ -1590,6 +1665,18 @@ _mysql_ConnectionObject_get_character_set_info(
1590
1665
check_connection (self );
1591
1666
mysql_get_character_set_info (& (self -> connection ), & cs );
1592
1667
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
1593
1680
if (cs .csname )
1594
1681
PyDict_SetItemString (result , "name" , PyString_FromString (cs .csname ));
1595
1682
if (cs .name )
@@ -1600,6 +1687,7 @@ _mysql_ConnectionObject_get_character_set_info(
1600
1687
PyDict_SetItemString (result , "dir" , PyString_FromString (cs .dir ));
1601
1688
PyDict_SetItemString (result , "mbminlen" , PyInt_FromLong (cs .mbminlen ));
1602
1689
PyDict_SetItemString (result , "mbmaxlen" , PyInt_FromLong (cs .mbmaxlen ));
1690
+ #endif
1603
1691
return result ;
1604
1692
}
1605
1693
#endif
@@ -1614,7 +1702,11 @@ _mysql_get_client_info(
1614
1702
{
1615
1703
if (!PyArg_ParseTuple (args , "" )) return NULL ;
1616
1704
check_server_init (NULL );
1705
+ #ifdef IS_PY3K
1706
+ return PyUnicode_FromString (mysql_get_client_info ());
1707
+ #else
1617
1708
return PyString_FromString (mysql_get_client_info ());
1709
+ #endif
1618
1710
}
1619
1711
1620
1712
static char _mysql_ConnectionObject_get_host_info__doc__ [] =
@@ -1629,7 +1721,11 @@ _mysql_ConnectionObject_get_host_info(
1629
1721
{
1630
1722
if (!PyArg_ParseTuple (args , "" )) return NULL ;
1631
1723
check_connection (self );
1724
+ #ifdef IS_PY3K
1725
+ return PyUnicode_FromString (mysql_get_host_info (& (self -> connection )));
1726
+ #else
1632
1727
return PyString_FromString (mysql_get_host_info (& (self -> connection )));
1728
+ #endif
1633
1729
}
1634
1730
1635
1731
static char _mysql_ConnectionObject_get_proto_info__doc__ [] =
@@ -1659,7 +1755,11 @@ _mysql_ConnectionObject_get_server_info(
1659
1755
{
1660
1756
if (!PyArg_ParseTuple (args , "" )) return NULL ;
1661
1757
check_connection (self );
1758
+ #ifdef IS_PY3K
1759
+ return PyUnicode_FromString (mysql_get_server_info (& (self -> connection )));
1760
+ #else
1662
1761
return PyString_FromString (mysql_get_server_info (& (self -> connection )));
1762
+ #endif
1663
1763
}
1664
1764
1665
1765
static char _mysql_ConnectionObject_info__doc__ [] =
@@ -1677,7 +1777,11 @@ _mysql_ConnectionObject_info(
1677
1777
if (!PyArg_ParseTuple (args , "" )) return NULL ;
1678
1778
check_connection (self );
1679
1779
s = mysql_info (& (self -> connection ));
1780
+ #ifdef IS_PY3K
1781
+ if (s ) return PyUnicode_FromString (s );
1782
+ #else
1680
1783
if (s ) return PyString_FromString (s );
1784
+ #endif
1681
1785
Py_INCREF (Py_None );
1682
1786
return Py_None ;
1683
1787
}
@@ -1920,7 +2024,12 @@ _mysql_ConnectionObject_stat(
1920
2024
s = mysql_stat (& (self -> connection ));
1921
2025
Py_END_ALLOW_THREADS
1922
2026
if (!s ) return _mysql_Exception (self );
2027
+ #ifdef IS_PY3K
2028
+ return PyUnicode_FromString (s );
2029
+ #else
1923
2030
return PyString_FromString (s );
2031
+ #endif
2032
+
1924
2033
}
1925
2034
1926
2035
static char _mysql_ConnectionObject_store_result__doc__ [] =
@@ -2046,7 +2155,11 @@ _mysql_ConnectionObject_repr(
2046
2155
else
2047
2156
sprintf (buf , "<_mysql.connection closed at %lx>" ,
2048
2157
(long )self );
2158
+ #ifdef IS_PY3K
2159
+ return PyUnicode_FromString (buf );
2160
+ #else
2049
2161
return PyString_FromString (buf );
2162
+ #endif
2050
2163
}
2051
2164
2052
2165
static char _mysql_ResultObject_data_seek__doc__ [] =
0 commit comments