Drop Python 2 support from C source (#406)

This commit is contained in:
Inada Naoki
2019-11-29 14:24:28 +09:00
committed by GitHub
parent 026e87f7b5
commit 86491882e6

View File

@ -36,14 +36,9 @@ PERFORMANCE OF THIS SOFTWARE.
#define PY_SSIZE_T_CLEAN 1
#include "Python.h"
#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#define PyInt_Type PyLong_Type
#define PyInt_FromString PyLong_FromString
#define PyInt_FromLong(n) PyLong_FromLong(n)
#define PyInt_Check(n) PyLong_Check(n)
#define PyInt_AS_LONG(n) PyLong_AS_LONG(n)
#define PyString_FromString(s) PyUnicode_FromString(s)
#if PY_MAJOR_VERSION == 2
#error "Python 2 is not supported"
#endif
#include "bytesobject.h"
@ -200,8 +195,8 @@ _mysql_Exception(_mysql_ConnectionObject *c)
e = _mysql_OperationalError;
break;
}
PyTuple_SET_ITEM(t, 0, PyInt_FromLong((long)merr));
PyTuple_SET_ITEM(t, 1, PyString_FromString(mysql_error(&(c->connection))));
PyTuple_SET_ITEM(t, 0, PyLong_FromLong((long)merr));
PyTuple_SET_ITEM(t, 1, PyUnicode_FromString(mysql_error(&(c->connection))));
PyErr_SetObject(e, t);
Py_DECREF(t);
return NULL;
@ -286,7 +281,7 @@ _mysql_ResultObject_Initialize(
fields = mysql_fetch_fields(result);
for (i=0; i<n; i++) {
PyObject *tmp, *fun;
tmp = PyInt_FromLong((long) fields[i].type);
tmp = PyLong_FromLong((long) fields[i].type);
if (!tmp) {
return -1;
}
@ -326,8 +321,8 @@ _mysql_ResultObject_Initialize(
pmask = PyTuple_GET_ITEM(t, 0);
fun2 = PyTuple_GET_ITEM(t, 1);
Py_XINCREF(fun2);
if (PyInt_Check(pmask)) {
mask = PyInt_AS_LONG(pmask);
if (PyLong_Check(pmask)) {
mask = PyLong_AS_LONG(pmask);
if (mask & flags) {
Py_DECREF(t);
break;
@ -434,15 +429,9 @@ _mysql_ConnectionObject_Initialize(
))
return -1;
#ifdef IS_PY3K
#define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\
if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
PyErr_Clear();}
#else
#define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\
if(t){d=PyString_AsString(t);ssl_keepref[n_ssl_keepref++]=t;}\
PyErr_Clear();}
#endif
if (ssl) {
PyObject *value = NULL;
@ -658,7 +647,7 @@ _mysql_ConnectionObject_affected_rows(
check_connection(self);
ret = mysql_affected_rows(&(self->connection));
if (ret == (my_ulonglong)-1)
return PyInt_FromLong(-1);
return PyLong_FromLong(-1);
return PyLong_FromUnsignedLongLong(ret);
}
@ -790,7 +779,7 @@ _mysql_ConnectionObject_next_result(
err = mysql_next_result(&(self->connection));
Py_END_ALLOW_THREADS
if (err > 0) return _mysql_Exception(self);
return PyInt_FromLong(err);
return PyLong_FromLong(err);
}
@ -813,7 +802,7 @@ _mysql_ConnectionObject_set_server_option(
err = mysql_set_server_option(&(self->connection), flags);
Py_END_ALLOW_THREADS
if (err) return _mysql_Exception(self);
return PyInt_FromLong(err);
return PyLong_FromLong(err);
}
static char _mysql_ConnectionObject_sqlstate__doc__[] =
@ -834,7 +823,7 @@ _mysql_ConnectionObject_sqlstate(
PyObject *noargs)
{
check_connection(self);
return PyString_FromString(mysql_sqlstate(&(self->connection)));
return PyUnicode_FromString(mysql_sqlstate(&(self->connection)));
}
static char _mysql_ConnectionObject_warning_count__doc__[] =
@ -849,7 +838,7 @@ _mysql_ConnectionObject_warning_count(
PyObject *noargs)
{
check_connection(self);
return PyInt_FromLong(mysql_warning_count(&(self->connection)));
return PyLong_FromLong(mysql_warning_count(&(self->connection)));
}
static char _mysql_ConnectionObject_errno__doc__[] =
@ -864,7 +853,7 @@ _mysql_ConnectionObject_errno(
PyObject *noargs)
{
check_connection(self);
return PyInt_FromLong((long)mysql_errno(&(self->connection)));
return PyLong_FromLong((long)mysql_errno(&(self->connection)));
}
static char _mysql_ConnectionObject_error__doc__[] =
@ -879,7 +868,7 @@ _mysql_ConnectionObject_error(
PyObject *noargs)
{
check_connection(self);
return PyString_FromString(mysql_error(&(self->connection)));
return PyUnicode_FromString(mysql_error(&(self->connection)));
}
static char _mysql_escape_string__doc__[] =
@ -948,14 +937,12 @@ _mysql_string_literal(
} else {
s = PyObject_Str(o);
if (!s) return NULL;
#ifdef IS_PY3K
{
PyObject *t = PyUnicode_AsASCIIString(s);
Py_DECREF(s);
if (!t) return NULL;
s = t;
}
#endif
}
in = PyBytes_AsString(s);
size = PyBytes_GET_SIZE(s);
@ -1055,7 +1042,6 @@ _mysql_ResultObject_describe(
if (!(d = PyTuple_New(n))) return NULL;
for (i=0; i<n; i++) {
PyObject *t;
#ifdef IS_PY3K
PyObject *name;
if (self->encoding == utf8) {
name = PyUnicode_DecodeUTF8(fields[i].name, fields[i].name_length, "replace");
@ -1068,10 +1054,6 @@ _mysql_ResultObject_describe(
t = Py_BuildValue("(Niiiiii)",
name,
#else
t = Py_BuildValue("(siiiiii)",
fields[i].name,
#endif
(long) fields[i].type,
(long) fields[i].max_length,
(long) fields[i].length,
@ -1105,7 +1087,7 @@ _mysql_ResultObject_field_flags(
if (!(d = PyTuple_New(n))) return NULL;
for (i=0; i<n; i++) {
PyObject *f;
if (!(f = PyInt_FromLong((long)fields[i].flags))) goto error;
if (!(f = PyLong_FromLong((long)fields[i].flags))) goto error;
PyTuple_SET_ITEM(d, i, f);
}
return d;
@ -1140,15 +1122,14 @@ _mysql_field_to_python(
//fprintf(stderr, "decoding with bytes\n", encoding);
return PyBytes_FromStringAndSize(rowitem, length);
}
if (converter == (PyObject*)&PyInt_Type) {
if (converter == (PyObject*)&PyLong_Type) {
//fprintf(stderr, "decoding with int\n", encoding);
return PyInt_FromString(rowitem, NULL, 10);
return PyLong_FromString(rowitem, NULL, 10);
}
//fprintf(stderr, "decoding with callback\n");
//PyObject_Print(converter, stderr, 0);
//fprintf(stderr, "\n");
#ifdef IS_PY3K
int binary;
switch (field->type) {
case FIELD_TYPE_TINY_BLOB:
@ -1174,10 +1155,6 @@ _mysql_field_to_python(
return PyObject_CallFunction(converter,
binary ? "y#" : "s#",
rowitem, (Py_ssize_t)length);
#else
return PyObject_CallFunction(converter,
"s#", rowitem, (Py_ssize_t)length);
#endif
}
static PyObject *
@ -1444,7 +1421,7 @@ _mysql_ConnectionObject_character_set_name(
const char *s;
check_connection(self);
s = mysql_character_set_name(&(self->connection));
return PyString_FromString(s);
return PyUnicode_FromString(s);
}
static char _mysql_ConnectionObject_set_character_set__doc__[] =
@ -1502,15 +1479,15 @@ _mysql_ConnectionObject_get_character_set_info(
mysql_get_character_set_info(&(self->connection), &cs);
if (!(result = PyDict_New())) return NULL;
if (cs.csname)
PyDict_SetItemString(result, "name", PyString_FromString(cs.csname));
PyDict_SetItemString(result, "name", PyUnicode_FromString(cs.csname));
if (cs.name)
PyDict_SetItemString(result, "collation", PyString_FromString(cs.name));
PyDict_SetItemString(result, "collation", PyUnicode_FromString(cs.name));
if (cs.comment)
PyDict_SetItemString(result, "comment", PyString_FromString(cs.comment));
PyDict_SetItemString(result, "comment", PyUnicode_FromString(cs.comment));
if (cs.dir)
PyDict_SetItemString(result, "dir", PyString_FromString(cs.dir));
PyDict_SetItemString(result, "mbminlen", PyInt_FromLong(cs.mbminlen));
PyDict_SetItemString(result, "mbmaxlen", PyInt_FromLong(cs.mbmaxlen));
PyDict_SetItemString(result, "dir", PyUnicode_FromString(cs.dir));
PyDict_SetItemString(result, "mbminlen", PyLong_FromLong(cs.mbminlen));
PyDict_SetItemString(result, "mbmaxlen", PyLong_FromLong(cs.mbmaxlen));
return result;
}
#endif
@ -1545,7 +1522,7 @@ _mysql_get_client_info(
PyObject *self,
PyObject *noargs)
{
return PyString_FromString(mysql_get_client_info());
return PyUnicode_FromString(mysql_get_client_info());
}
static char _mysql_ConnectionObject_get_host_info__doc__[] =
@ -1559,7 +1536,7 @@ _mysql_ConnectionObject_get_host_info(
PyObject *noargs)
{
check_connection(self);
return PyString_FromString(mysql_get_host_info(&(self->connection)));
return PyUnicode_FromString(mysql_get_host_info(&(self->connection)));
}
static char _mysql_ConnectionObject_get_proto_info__doc__[] =
@ -1573,7 +1550,7 @@ _mysql_ConnectionObject_get_proto_info(
PyObject *noargs)
{
check_connection(self);
return PyInt_FromLong((long)mysql_get_proto_info(&(self->connection)));
return PyLong_FromLong((long)mysql_get_proto_info(&(self->connection)));
}
static char _mysql_ConnectionObject_get_server_info__doc__[] =
@ -1587,7 +1564,7 @@ _mysql_ConnectionObject_get_server_info(
PyObject *noargs)
{
check_connection(self);
return PyString_FromString(mysql_get_server_info(&(self->connection)));
return PyUnicode_FromString(mysql_get_server_info(&(self->connection)));
}
static char _mysql_ConnectionObject_info__doc__[] =
@ -1604,7 +1581,7 @@ _mysql_ConnectionObject_info(
const char *s;
check_connection(self);
s = mysql_info(&(self->connection));
if (s) return PyString_FromString(s);
if (s) return PyUnicode_FromString(s);
Py_RETURN_NONE;
}
@ -1674,7 +1651,7 @@ _mysql_ConnectionObject_field_count(
PyObject *noargs)
{
check_connection(self);
return PyInt_FromLong((long)mysql_field_count(&(self->connection)));
return PyLong_FromLong((long)mysql_field_count(&(self->connection)));
}
static char _mysql_ConnectionObject_fileno__doc__[] =
@ -1688,7 +1665,7 @@ _mysql_ConnectionObject_fileno(
PyObject *noargs)
{
check_connection(self);
return PyInt_FromLong(self->connection.net.fd);
return PyLong_FromLong(self->connection.net.fd);
}
static char _mysql_ResultObject_num_fields__doc__[] =
@ -1700,7 +1677,7 @@ _mysql_ResultObject_num_fields(
PyObject *noargs)
{
check_result_connection(self);
return PyInt_FromLong((long)mysql_num_fields(self->result));
return PyLong_FromLong((long)mysql_num_fields(self->result));
}
static char _mysql_ResultObject_num_rows__doc__[] =
@ -1889,7 +1866,7 @@ _mysql_ConnectionObject_stat(
s = mysql_stat(&(self->connection));
Py_END_ALLOW_THREADS
if (!s) return _mysql_Exception(self);
return PyString_FromString(s);
return PyUnicode_FromString(s);
}
static char _mysql_ConnectionObject_store_result__doc__[] =
@ -1950,7 +1927,7 @@ _mysql_ConnectionObject_thread_id(
Py_BEGIN_ALLOW_THREADS
pid = mysql_thread_id(&(self->connection));
Py_END_ALLOW_THREADS
return PyInt_FromLong((long)pid);
return PyLong_FromLong((long)pid);
}
static char _mysql_ConnectionObject_use_result__doc__[] =
@ -2013,7 +1990,7 @@ _mysql_ConnectionObject_repr(
self->connection.host, self);
else
snprintf(buf, 300, "<_mysql.connection closed at %p>", self);
return PyString_FromString(buf);
return PyUnicode_FromString(buf);
}
static char _mysql_ResultObject_data_seek__doc__[] =
@ -2046,7 +2023,7 @@ _mysql_ResultObject_repr(
{
char buf[300];
snprintf(buf, 300, "<_mysql.result object at %p>", self);
return PyString_FromString(buf);
return PyUnicode_FromString(buf);
}
static PyMethodDef _mysql_ConnectionObject_methods[] = {
@ -2391,13 +2368,9 @@ _mysql_ConnectionObject_getattro(
PyObject *name)
{
const char *cname;
#ifdef IS_PY3K
cname = PyUnicode_AsUTF8(name);
#else
cname = PyString_AsString(name);
#endif
if (strcmp(cname, "closed") == 0)
return PyInt_FromLong((long)!(self->open));
return PyLong_FromLong((long)!(self->open));
return PyObject_GenericGetAttr((PyObject *)self, name);
}
@ -2638,7 +2611,6 @@ an argument are now methods of the result object. Deprecated functions\n\
(as of 3.23) are NOT implemented.\n\
";
#ifdef IS_PY3K
static struct PyModuleDef _mysqlmodule = {
PyModuleDef_HEAD_INIT,
"_mysql", /* name of module */
@ -2650,23 +2622,14 @@ static struct PyModuleDef _mysqlmodule = {
PyMODINIT_FUNC
PyInit__mysql(void)
#else
DL_EXPORT(void)
init_mysql(void)
#endif
{
PyObject *dict, *module, *emod, *edict;
if (mysql_library_init(0, NULL, NULL)) {
PyErr_SetString(PyExc_ImportError, "_mysql: mysql_library_init failed");
#ifdef IS_PY3K
return NULL;
#else
return;
#endif
}
#ifdef IS_PY3K
if (PyType_Ready(&_mysql_ConnectionObject_Type) < 0)
return NULL;
if (PyType_Ready(&_mysql_ResultObject_Type) < 0)
@ -2674,15 +2637,6 @@ init_mysql(void)
module = PyModule_Create(&_mysqlmodule);
if (!module) return module; /* this really should never happen */
#else
if (PyType_Ready(&_mysql_ConnectionObject_Type) < 0)
return;
if (PyType_Ready(&_mysql_ResultObject_Type) < 0)
return;
module = Py_InitModule4("_mysql", _mysql_methods, _mysql___doc__,
(PyObject *)NULL, PYTHON_API_VERSION);
if (!module) return; /* this really should never happen */
#endif
if (!(dict = PyModule_GetDict(module))) goto error;
if (PyDict_SetItemString(dict, "version_info",
@ -2690,7 +2644,7 @@ init_mysql(void)
dict, dict)))
goto error;
if (PyDict_SetItemString(dict, "__version__",
PyString_FromString(QUOTE(__version__))))
PyUnicode_FromString(QUOTE(__version__))))
goto error;
if (PyDict_SetItemString(dict, "connection",
(PyObject *)&_mysql_ConnectionObject_Type))
@ -2744,9 +2698,7 @@ init_mysql(void)
PyErr_SetString(PyExc_ImportError, "_mysql: init failed");
module = NULL;
}
#ifdef IS_PY3K
return module;
#endif
}
/* vim: set ts=4 sts=4 sw=4 expandtab : */