mirror of
https://github.com/PyMySQL/mysqlclient.git
synced 2025-08-15 02:54:29 +08:00
A lot more Python 3 fixes for _mysql. It still ain't fixed or broken yet (for Python <3)
This commit is contained in:
119
MySQLdb/_mysql.c
119
MySQLdb/_mysql.c
@ -26,12 +26,15 @@ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|||||||
PERFORMANCE OF THIS SOFTWARE.
|
PERFORMANCE OF THIS SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "Python.h"
|
||||||
#if PY_MAJOR_VERSION >= 3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
#define IS_PY3K
|
#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
|
#endif
|
||||||
#if PY_VERSION_HEX > 0x02060000
|
#if PY_VERSION_HEX > 0x02060000
|
||||||
#include "bytesobject.h"
|
#include "bytesobject.h"
|
||||||
#include "intobject.h"
|
|
||||||
#endif
|
#endif
|
||||||
#include "pymemcompat.h"
|
#include "pymemcompat.h"
|
||||||
#include "structmember.h"
|
#include "structmember.h"
|
||||||
@ -121,7 +124,11 @@ _mysql_Exception(_mysql_ConnectionObject *c)
|
|||||||
if (!_mysql_server_init_done) {
|
if (!_mysql_server_init_done) {
|
||||||
e = _mysql_InternalError;
|
e = _mysql_InternalError;
|
||||||
PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L));
|
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"));
|
PyTuple_SET_ITEM(t, 1, PyString_FromString("server not initialized"));
|
||||||
|
#endif
|
||||||
PyErr_SetObject(e, t);
|
PyErr_SetObject(e, t);
|
||||||
Py_DECREF(t);
|
Py_DECREF(t);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -131,7 +138,11 @@ _mysql_Exception(_mysql_ConnectionObject *c)
|
|||||||
e = _mysql_InterfaceError;
|
e = _mysql_InterfaceError;
|
||||||
else if (merr > CR_MAX_ERROR) {
|
else if (merr > CR_MAX_ERROR) {
|
||||||
PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L));
|
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"));
|
PyTuple_SET_ITEM(t, 1, PyString_FromString("error totally whack"));
|
||||||
|
#endif
|
||||||
PyErr_SetObject(_mysql_InterfaceError, t);
|
PyErr_SetObject(_mysql_InterfaceError, t);
|
||||||
Py_DECREF(t);
|
Py_DECREF(t);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -219,7 +230,11 @@ _mysql_Exception(_mysql_ConnectionObject *c)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
PyTuple_SET_ITEM(t, 0, PyInt_FromLong((long)merr));
|
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))));
|
PyTuple_SET_ITEM(t, 1, PyString_FromString(mysql_error(&(c->connection))));
|
||||||
|
#endif
|
||||||
PyErr_SetObject(e, t);
|
PyErr_SetObject(e, t);
|
||||||
Py_DECREF(t);
|
Py_DECREF(t);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -268,7 +283,12 @@ static PyObject *_mysql_server_init(
|
|||||||
cmd_args_c = (char **) PyMem_Malloc(cmd_argc*sizeof(char *));
|
cmd_args_c = (char **) PyMem_Malloc(cmd_argc*sizeof(char *));
|
||||||
for (i=0; i< cmd_argc; i++) {
|
for (i=0; i< cmd_argc; i++) {
|
||||||
item = PySequence_GetItem(cmd_args, i);
|
item = PySequence_GetItem(cmd_args, i);
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
s = PyUnicode_AS_DATA(item);
|
||||||
|
#else
|
||||||
s = PyString_AsString(item);
|
s = PyString_AsString(item);
|
||||||
|
#endif
|
||||||
|
|
||||||
Py_DECREF(item);
|
Py_DECREF(item);
|
||||||
if (!s) {
|
if (!s) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
@ -293,7 +313,11 @@ static PyObject *_mysql_server_init(
|
|||||||
groups_c = (char **) PyMem_Malloc((1+groupc)*sizeof(char *));
|
groups_c = (char **) PyMem_Malloc((1+groupc)*sizeof(char *));
|
||||||
for (i=0; i< groupc; i++) {
|
for (i=0; i< groupc; i++) {
|
||||||
item = PySequence_GetItem(groups, i);
|
item = PySequence_GetItem(groups, i);
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
s = PyUnicode_AS_DATA(item);
|
||||||
|
#else
|
||||||
s = PyString_AsString(item);
|
s = PyString_AsString(item);
|
||||||
|
#endif
|
||||||
Py_DECREF(item);
|
Py_DECREF(item);
|
||||||
if (!s) {
|
if (!s) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
@ -527,9 +551,15 @@ _mysql_ConnectionObject_Initialize(
|
|||||||
))
|
))
|
||||||
return -1;
|
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);\
|
#define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\
|
||||||
if(t){d=PyString_AsString(t);Py_DECREF(t);}\
|
if(t){d=PyString_AsString(t);Py_DECREF(t);}\
|
||||||
PyErr_Clear();}
|
PyErr_Clear();}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (ssl) {
|
if (ssl) {
|
||||||
#if HAVE_OPENSSL
|
#if HAVE_OPENSSL
|
||||||
@ -919,8 +949,12 @@ _mysql_ConnectionObject_sqlstate(
|
|||||||
PyObject *args)
|
PyObject *args)
|
||||||
{
|
{
|
||||||
if (!PyArg_ParseTuple(args, "")) return NULL;
|
if (!PyArg_ParseTuple(args, "")) return NULL;
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
return PyUnicode_FromString(mysql_sqlstate(&(self->connection)));
|
||||||
|
#else
|
||||||
return PyString_FromString(mysql_sqlstate(&(self->connection)));
|
return PyString_FromString(mysql_sqlstate(&(self->connection)));
|
||||||
}
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
static char _mysql_ConnectionObject_warning_count__doc__[] =
|
static char _mysql_ConnectionObject_warning_count__doc__[] =
|
||||||
"Returns the number of warnings generated during execution\n\
|
"Returns the number of warnings generated during execution\n\
|
||||||
@ -968,7 +1002,11 @@ _mysql_ConnectionObject_error(
|
|||||||
{
|
{
|
||||||
if (!PyArg_ParseTuple(args, "")) return NULL;
|
if (!PyArg_ParseTuple(args, "")) return NULL;
|
||||||
check_connection(self);
|
check_connection(self);
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
return PyUnicode_FromString(mysql_error(&(self->connection)));
|
||||||
|
#else
|
||||||
return PyString_FromString(mysql_error(&(self->connection)));
|
return PyString_FromString(mysql_error(&(self->connection)));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static char _mysql_escape_string__doc__[] =
|
static char _mysql_escape_string__doc__[] =
|
||||||
@ -988,9 +1026,17 @@ _mysql_escape_string(
|
|||||||
char *in, *out;
|
char *in, *out;
|
||||||
int len, size;
|
int len, size;
|
||||||
if (!PyArg_ParseTuple(args, "s#:escape_string", &in, &size)) return NULL;
|
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);
|
str = PyString_FromStringAndSize((char *) NULL, size*2+1);
|
||||||
|
#endif
|
||||||
if (!str) return PyErr_NoMemory();
|
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
|
#if MYSQL_VERSION_ID < 32321
|
||||||
len = mysql_escape_string(out, in, size);
|
len = mysql_escape_string(out, in, size);
|
||||||
#else
|
#else
|
||||||
@ -1000,7 +1046,11 @@ _mysql_escape_string(
|
|||||||
else
|
else
|
||||||
len = mysql_escape_string(out, in, size);
|
len = mysql_escape_string(out, in, size);
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
if (PyUnicode_Resize(&str, len) < 0) return NULL;
|
||||||
|
#else
|
||||||
if (_PyString_Resize(&str, len) < 0) return NULL;
|
if (_PyString_Resize(&str, len) < 0) return NULL;
|
||||||
|
#endif
|
||||||
return (str);
|
return (str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1025,11 +1075,19 @@ _mysql_string_literal(
|
|||||||
if (!PyArg_ParseTuple(args, "O|O:string_literal", &o, &d)) return NULL;
|
if (!PyArg_ParseTuple(args, "O|O:string_literal", &o, &d)) return NULL;
|
||||||
s = PyObject_Str(o);
|
s = PyObject_Str(o);
|
||||||
if (!s) return NULL;
|
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);
|
in = PyString_AsString(s);
|
||||||
size = PyString_GET_SIZE(s);
|
size = PyString_GET_SIZE(s);
|
||||||
str = PyString_FromStringAndSize((char *) NULL, size*2+3);
|
str = PyString_FromStringAndSize((char *) NULL, size*2+3);
|
||||||
if (!str) return PyErr_NoMemory();
|
if (!str) return PyErr_NoMemory();
|
||||||
out = PyString_AS_STRING(str);
|
out = PyString_AS_STRING(str);
|
||||||
|
#endif
|
||||||
#if MYSQL_VERSION_ID < 32321
|
#if MYSQL_VERSION_ID < 32321
|
||||||
len = mysql_escape_string(out+1, in, size);
|
len = mysql_escape_string(out+1, in, size);
|
||||||
#else
|
#else
|
||||||
@ -1040,7 +1098,11 @@ _mysql_string_literal(
|
|||||||
len = mysql_escape_string(out+1, in, size);
|
len = mysql_escape_string(out+1, in, size);
|
||||||
#endif
|
#endif
|
||||||
*out = *(out+len+1) = '\'';
|
*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;
|
if (_PyString_Resize(&str, len+2) < 0) return NULL;
|
||||||
|
#endif
|
||||||
Py_DECREF(s);
|
Py_DECREF(s);
|
||||||
return (str);
|
return (str);
|
||||||
}
|
}
|
||||||
@ -1060,7 +1122,11 @@ _escape_item(
|
|||||||
if (!itemconv) {
|
if (!itemconv) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
itemconv = PyObject_GetItem(d,
|
itemconv = PyObject_GetItem(d,
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
(PyObject *) &PyUnicode_Type);
|
||||||
|
#else
|
||||||
(PyObject *) &PyString_Type);
|
(PyObject *) &PyString_Type);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if (!itemconv) {
|
if (!itemconv) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
@ -1247,8 +1313,13 @@ _mysql_field_to_python(
|
|||||||
rowitem,
|
rowitem,
|
||||||
(int)length);
|
(int)length);
|
||||||
else
|
else
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
v = PyUnicode_FromStringAndSize(rowitem,
|
||||||
|
(int)length);
|
||||||
|
#else
|
||||||
v = PyString_FromStringAndSize(rowitem,
|
v = PyString_FromStringAndSize(rowitem,
|
||||||
(int)length);
|
(int)length);
|
||||||
|
#endif
|
||||||
if (!v)
|
if (!v)
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
@ -1529,7 +1600,11 @@ _mysql_ConnectionObject_character_set_name(
|
|||||||
#else
|
#else
|
||||||
s = "latin1";
|
s = "latin1";
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
return PyUnicode_FromString(s);
|
||||||
|
#else
|
||||||
return PyString_FromString(s);
|
return PyString_FromString(s);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#if MYSQL_VERSION_ID >= 50007
|
#if MYSQL_VERSION_ID >= 50007
|
||||||
@ -1590,6 +1665,18 @@ _mysql_ConnectionObject_get_character_set_info(
|
|||||||
check_connection(self);
|
check_connection(self);
|
||||||
mysql_get_character_set_info(&(self->connection), &cs);
|
mysql_get_character_set_info(&(self->connection), &cs);
|
||||||
if (!(result = PyDict_New())) return NULL;
|
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)
|
if (cs.csname)
|
||||||
PyDict_SetItemString(result, "name", PyString_FromString(cs.csname));
|
PyDict_SetItemString(result, "name", PyString_FromString(cs.csname));
|
||||||
if (cs.name)
|
if (cs.name)
|
||||||
@ -1600,6 +1687,7 @@ _mysql_ConnectionObject_get_character_set_info(
|
|||||||
PyDict_SetItemString(result, "dir", PyString_FromString(cs.dir));
|
PyDict_SetItemString(result, "dir", PyString_FromString(cs.dir));
|
||||||
PyDict_SetItemString(result, "mbminlen", PyInt_FromLong(cs.mbminlen));
|
PyDict_SetItemString(result, "mbminlen", PyInt_FromLong(cs.mbminlen));
|
||||||
PyDict_SetItemString(result, "mbmaxlen", PyInt_FromLong(cs.mbmaxlen));
|
PyDict_SetItemString(result, "mbmaxlen", PyInt_FromLong(cs.mbmaxlen));
|
||||||
|
#endif
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -1614,7 +1702,11 @@ _mysql_get_client_info(
|
|||||||
{
|
{
|
||||||
if (!PyArg_ParseTuple(args, "")) return NULL;
|
if (!PyArg_ParseTuple(args, "")) return NULL;
|
||||||
check_server_init(NULL);
|
check_server_init(NULL);
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
return PyUnicode_FromString(mysql_get_client_info());
|
||||||
|
#else
|
||||||
return PyString_FromString(mysql_get_client_info());
|
return PyString_FromString(mysql_get_client_info());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static char _mysql_ConnectionObject_get_host_info__doc__[] =
|
static char _mysql_ConnectionObject_get_host_info__doc__[] =
|
||||||
@ -1629,7 +1721,11 @@ _mysql_ConnectionObject_get_host_info(
|
|||||||
{
|
{
|
||||||
if (!PyArg_ParseTuple(args, "")) return NULL;
|
if (!PyArg_ParseTuple(args, "")) return NULL;
|
||||||
check_connection(self);
|
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)));
|
return PyString_FromString(mysql_get_host_info(&(self->connection)));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static char _mysql_ConnectionObject_get_proto_info__doc__[] =
|
static char _mysql_ConnectionObject_get_proto_info__doc__[] =
|
||||||
@ -1659,7 +1755,11 @@ _mysql_ConnectionObject_get_server_info(
|
|||||||
{
|
{
|
||||||
if (!PyArg_ParseTuple(args, "")) return NULL;
|
if (!PyArg_ParseTuple(args, "")) return NULL;
|
||||||
check_connection(self);
|
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)));
|
return PyString_FromString(mysql_get_server_info(&(self->connection)));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static char _mysql_ConnectionObject_info__doc__[] =
|
static char _mysql_ConnectionObject_info__doc__[] =
|
||||||
@ -1677,7 +1777,11 @@ _mysql_ConnectionObject_info(
|
|||||||
if (!PyArg_ParseTuple(args, "")) return NULL;
|
if (!PyArg_ParseTuple(args, "")) return NULL;
|
||||||
check_connection(self);
|
check_connection(self);
|
||||||
s = mysql_info(&(self->connection));
|
s = mysql_info(&(self->connection));
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
if (s) return PyUnicode_FromString(s);
|
||||||
|
#else
|
||||||
if (s) return PyString_FromString(s);
|
if (s) return PyString_FromString(s);
|
||||||
|
#endif
|
||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return Py_None;
|
return Py_None;
|
||||||
}
|
}
|
||||||
@ -1920,7 +2024,12 @@ _mysql_ConnectionObject_stat(
|
|||||||
s = mysql_stat(&(self->connection));
|
s = mysql_stat(&(self->connection));
|
||||||
Py_END_ALLOW_THREADS
|
Py_END_ALLOW_THREADS
|
||||||
if (!s) return _mysql_Exception(self);
|
if (!s) return _mysql_Exception(self);
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
return PyUnicode_FromString(s);
|
||||||
|
#else
|
||||||
return PyString_FromString(s);
|
return PyString_FromString(s);
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static char _mysql_ConnectionObject_store_result__doc__[] =
|
static char _mysql_ConnectionObject_store_result__doc__[] =
|
||||||
@ -2046,7 +2155,11 @@ _mysql_ConnectionObject_repr(
|
|||||||
else
|
else
|
||||||
sprintf(buf, "<_mysql.connection closed at %lx>",
|
sprintf(buf, "<_mysql.connection closed at %lx>",
|
||||||
(long)self);
|
(long)self);
|
||||||
|
#ifdef IS_PY3K
|
||||||
|
return PyUnicode_FromString(buf);
|
||||||
|
#else
|
||||||
return PyString_FromString(buf);
|
return PyString_FromString(buf);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static char _mysql_ResultObject_data_seek__doc__[] =
|
static char _mysql_ResultObject_data_seek__doc__[] =
|
||||||
|
Reference in New Issue
Block a user