diff options
author | adustman <adustman@9fc6cd9a-920d-0410-adcf-ac96716ed7e8> | 2012-09-07 22:09:31 +0000 |
---|---|---|
committer | adustman <adustman@9fc6cd9a-920d-0410-adcf-ac96716ed7e8> | 2012-09-07 22:09:31 +0000 |
commit | 66abb0e8d09ebca685c2e0f91b5ed16019bdd990 (patch) | |
tree | 54efb8a4a23274799d44dbf5f472081aa8b9dc7b | |
parent | 9737a4cb39df8ac28e52382e0ce844d6e83b6c40 (diff) | |
download | mysqldb1-66abb0e8d09ebca685c2e0f91b5ed16019bdd990.tar.gz |
A lot more Python 3 fixes for _mysql. It still ain't fixed or broken yet (for Python <3)
-rw-r--r-- | MySQLdb/_mysql.c | 119 |
1 files changed, 116 insertions, 3 deletions
diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 0e1f0bb..e79303b 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -26,12 +26,15 @@ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "Python.h" #if PY_MAJOR_VERSION >= 3 #define IS_PY3K +#define PyInt_FromLong(n) PyLong_FromLong(n) +#define PyInt_Check(n) PyLong_Check(n) +#define PyInt_AS_LONG(n) PyLong_AS_LONG(n) #endif #if PY_VERSION_HEX > 0x02060000 #include "bytesobject.h" -#include "intobject.h" #endif #include "pymemcompat.h" #include "structmember.h" @@ -121,7 +124,11 @@ _mysql_Exception(_mysql_ConnectionObject *c) if (!_mysql_server_init_done) { e = _mysql_InternalError; PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L)); +#ifdef IS_PY3K + PyTuple_SET_ITEM(t, 1, PyUnicode_FromString("server not initialized")); +#else PyTuple_SET_ITEM(t, 1, PyString_FromString("server not initialized")); +#endif PyErr_SetObject(e, t); Py_DECREF(t); return NULL; @@ -131,7 +138,11 @@ _mysql_Exception(_mysql_ConnectionObject *c) e = _mysql_InterfaceError; else if (merr > CR_MAX_ERROR) { PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L)); +#ifdef IS_PY3K + PyTuple_SET_ITEM(t, 1, PyUnicode_FromString("error totally whack")); +#else PyTuple_SET_ITEM(t, 1, PyString_FromString("error totally whack")); +#endif PyErr_SetObject(_mysql_InterfaceError, t); Py_DECREF(t); return NULL; @@ -219,7 +230,11 @@ _mysql_Exception(_mysql_ConnectionObject *c) break; } PyTuple_SET_ITEM(t, 0, PyInt_FromLong((long)merr)); +#ifdef IS_PY3K + PyTuple_SET_ITEM(t, 1, PyUnicode_FromString(mysql_error(&(c->connection)))); +#else PyTuple_SET_ITEM(t, 1, PyString_FromString(mysql_error(&(c->connection)))); +#endif PyErr_SetObject(e, t); Py_DECREF(t); return NULL; @@ -268,7 +283,12 @@ static PyObject *_mysql_server_init( cmd_args_c = (char **) PyMem_Malloc(cmd_argc*sizeof(char *)); for (i=0; i< cmd_argc; i++) { item = PySequence_GetItem(cmd_args, i); +#ifdef IS_PY3K + s = PyUnicode_AS_DATA(item); +#else s = PyString_AsString(item); +#endif + Py_DECREF(item); if (!s) { PyErr_SetString(PyExc_TypeError, @@ -293,7 +313,11 @@ static PyObject *_mysql_server_init( groups_c = (char **) PyMem_Malloc((1+groupc)*sizeof(char *)); for (i=0; i< groupc; i++) { item = PySequence_GetItem(groups, i); +#ifdef IS_PY3K + s = PyUnicode_AS_DATA(item); +#else s = PyString_AsString(item); +#endif Py_DECREF(item); if (!s) { PyErr_SetString(PyExc_TypeError, @@ -527,9 +551,15 @@ _mysql_ConnectionObject_Initialize( )) return -1; +#ifdef IS_PY3K +#define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\ + if(t){d=PyUnicode_AS_DATA(t);Py_DECREF(t);}\ + PyErr_Clear();} +#else #define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\ if(t){d=PyString_AsString(t);Py_DECREF(t);}\ PyErr_Clear();} +#endif if (ssl) { #if HAVE_OPENSSL @@ -919,8 +949,12 @@ _mysql_ConnectionObject_sqlstate( PyObject *args) { if (!PyArg_ParseTuple(args, "")) return NULL; +#ifdef IS_PY3K + return PyUnicode_FromString(mysql_sqlstate(&(self->connection))); +#else return PyString_FromString(mysql_sqlstate(&(self->connection))); -} +#endif +} static char _mysql_ConnectionObject_warning_count__doc__[] = "Returns the number of warnings generated during execution\n\ @@ -968,7 +1002,11 @@ _mysql_ConnectionObject_error( { if (!PyArg_ParseTuple(args, "")) return NULL; check_connection(self); +#ifdef IS_PY3K + return PyUnicode_FromString(mysql_error(&(self->connection))); +#else return PyString_FromString(mysql_error(&(self->connection))); +#endif } static char _mysql_escape_string__doc__[] = @@ -988,9 +1026,17 @@ _mysql_escape_string( char *in, *out; int len, size; if (!PyArg_ParseTuple(args, "s#:escape_string", &in, &size)) return NULL; +#ifdef IS_PY3K + str = PyUnicode_FromStringAndSize((char *) NULL, size*2+1); +#else str = PyString_FromStringAndSize((char *) NULL, size*2+1); +#endif if (!str) return PyErr_NoMemory(); - out = PyString_AS_STRING(str); +#ifdef IS_PY3K + out = PyUnicode_AS_DATA(str); +#else + out = PyString_AS_STRING(str); +#endif #if MYSQL_VERSION_ID < 32321 len = mysql_escape_string(out, in, size); #else @@ -1000,7 +1046,11 @@ _mysql_escape_string( else len = mysql_escape_string(out, in, size); #endif +#ifdef IS_PY3K + if (PyUnicode_Resize(&str, len) < 0) return NULL; +#else if (_PyString_Resize(&str, len) < 0) return NULL; +#endif return (str); } @@ -1025,11 +1075,19 @@ _mysql_string_literal( if (!PyArg_ParseTuple(args, "O|O:string_literal", &o, &d)) return NULL; s = PyObject_Str(o); if (!s) return NULL; +#ifdef IS_PY3K + in = PyUnicode_AS_DATA(s); + size = PyUnicode_GetSize(s); + str = PyUnicode_FromStringAndSize((char *) NULL, size*2+3); + if (!str) return PyErr_NoMemory(); + out = PyUnicode_AS_DATA(str); +#else in = PyString_AsString(s); size = PyString_GET_SIZE(s); str = PyString_FromStringAndSize((char *) NULL, size*2+3); if (!str) return PyErr_NoMemory(); out = PyString_AS_STRING(str); +#endif #if MYSQL_VERSION_ID < 32321 len = mysql_escape_string(out+1, in, size); #else @@ -1040,7 +1098,11 @@ _mysql_string_literal( len = mysql_escape_string(out+1, in, size); #endif *out = *(out+len+1) = '\''; +#ifdef IS_PY3K + if (PyUnicode_Resize(&str, len+2) < 0) return NULL; +#else if (_PyString_Resize(&str, len+2) < 0) return NULL; +#endif Py_DECREF(s); return (str); } @@ -1060,7 +1122,11 @@ _escape_item( if (!itemconv) { PyErr_Clear(); itemconv = PyObject_GetItem(d, +#ifdef IS_PY3K + (PyObject *) &PyUnicode_Type); +#else (PyObject *) &PyString_Type); +#endif } if (!itemconv) { PyErr_SetString(PyExc_TypeError, @@ -1247,8 +1313,13 @@ _mysql_field_to_python( rowitem, (int)length); else +#ifdef IS_PY3K + v = PyUnicode_FromStringAndSize(rowitem, + (int)length); +#else v = PyString_FromStringAndSize(rowitem, (int)length); +#endif if (!v) return NULL; } else { @@ -1529,7 +1600,11 @@ _mysql_ConnectionObject_character_set_name( #else s = "latin1"; #endif +#ifdef IS_PY3K + return PyUnicode_FromString(s); +#else return PyString_FromString(s); +#endif } #if MYSQL_VERSION_ID >= 50007 @@ -1590,6 +1665,18 @@ _mysql_ConnectionObject_get_character_set_info( check_connection(self); mysql_get_character_set_info(&(self->connection), &cs); if (!(result = PyDict_New())) return NULL; +#ifdef IS_PY3K + if (cs.csname) + PyDict_SetItemString(result, "name", PyUnicode_FromString(cs.csname)); + if (cs.name) + PyDict_SetItemString(result, "collation", PyUnicode_FromString(cs.name)); + if (cs.comment) + PyDict_SetItemString(result, "comment", PyUnicode_FromString(cs.comment)); + if (cs.dir) + PyDict_SetItemString(result, "dir", PyUnicode_FromString(cs.dir)); + PyDict_SetItemString(result, "mbminlen", PyInt_FromLong(cs.mbminlen)); + PyDict_SetItemString(result, "mbmaxlen", PyInt_FromLong(cs.mbmaxlen)); +#else if (cs.csname) PyDict_SetItemString(result, "name", PyString_FromString(cs.csname)); if (cs.name) @@ -1600,6 +1687,7 @@ _mysql_ConnectionObject_get_character_set_info( PyDict_SetItemString(result, "dir", PyString_FromString(cs.dir)); PyDict_SetItemString(result, "mbminlen", PyInt_FromLong(cs.mbminlen)); PyDict_SetItemString(result, "mbmaxlen", PyInt_FromLong(cs.mbmaxlen)); +#endif return result; } #endif @@ -1614,7 +1702,11 @@ _mysql_get_client_info( { if (!PyArg_ParseTuple(args, "")) return NULL; check_server_init(NULL); +#ifdef IS_PY3K + return PyUnicode_FromString(mysql_get_client_info()); +#else return PyString_FromString(mysql_get_client_info()); +#endif } static char _mysql_ConnectionObject_get_host_info__doc__[] = @@ -1629,7 +1721,11 @@ _mysql_ConnectionObject_get_host_info( { if (!PyArg_ParseTuple(args, "")) return NULL; check_connection(self); +#ifdef IS_PY3K + return PyUnicode_FromString(mysql_get_host_info(&(self->connection))); +#else return PyString_FromString(mysql_get_host_info(&(self->connection))); +#endif } static char _mysql_ConnectionObject_get_proto_info__doc__[] = @@ -1659,7 +1755,11 @@ _mysql_ConnectionObject_get_server_info( { if (!PyArg_ParseTuple(args, "")) return NULL; check_connection(self); +#ifdef IS_PY3K + return PyUnicode_FromString(mysql_get_server_info(&(self->connection))); +#else return PyString_FromString(mysql_get_server_info(&(self->connection))); +#endif } static char _mysql_ConnectionObject_info__doc__[] = @@ -1677,7 +1777,11 @@ _mysql_ConnectionObject_info( if (!PyArg_ParseTuple(args, "")) return NULL; check_connection(self); s = mysql_info(&(self->connection)); +#ifdef IS_PY3K + if (s) return PyUnicode_FromString(s); +#else if (s) return PyString_FromString(s); +#endif Py_INCREF(Py_None); return Py_None; } @@ -1920,7 +2024,12 @@ _mysql_ConnectionObject_stat( s = mysql_stat(&(self->connection)); Py_END_ALLOW_THREADS if (!s) return _mysql_Exception(self); +#ifdef IS_PY3K + return PyUnicode_FromString(s); +#else return PyString_FromString(s); +#endif + } static char _mysql_ConnectionObject_store_result__doc__[] = @@ -2046,7 +2155,11 @@ _mysql_ConnectionObject_repr( else sprintf(buf, "<_mysql.connection closed at %lx>", (long)self); +#ifdef IS_PY3K + return PyUnicode_FromString(buf); +#else return PyString_FromString(buf); +#endif } static char _mysql_ResultObject_data_seek__doc__[] = |