Skip to content

Commit b5780d2

Browse files
committed
self of module-level function is not NULL on Python 3.
1 parent c66b43c commit b5780d2

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

_mysql.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,9 @@ _mysql_escape_string(
10741074
len = mysql_escape_string(out, in, size);
10751075
#else
10761076
check_server_init(NULL);
1077+
1078+
if (PyModule_Check((PyObject*)self))
1079+
self = NULL;
10771080
if (self && self->open)
10781081
len = mysql_real_escape_string(&(self->connection), out, in, size);
10791082
else
@@ -1101,21 +1104,31 @@ _mysql_string_literal(
11011104
PyObject *str, *s, *o, *d;
11021105
char *in, *out;
11031106
int len, size;
1107+
if (PyModule_Check((PyObject*)self))
1108+
self = NULL;
11041109
if (!PyArg_ParseTuple(args, "O|O:string_literal", &o, &d)) return NULL;
1105-
s = PyObject_Str(o);
1106-
if (!s) return NULL;
1110+
if (PyBytes_Check(o)) {
1111+
s = o;
1112+
Py_INCREF(s);
1113+
} else {
1114+
s = PyObject_Str(o);
1115+
if (!s) return NULL;
11071116
#ifdef IS_PY3K
1108-
{
1109-
PyObject *t = PyUnicode_AsASCIIString(s);
1110-
if (!t) return NULL;
1111-
Py_DECREF(s);
1112-
s = t;
1113-
}
1117+
{
1118+
PyObject *t = PyUnicode_AsASCIIString(s);
1119+
Py_DECREF(s);
1120+
if (!t) return NULL;
1121+
s = t;
1122+
}
11141123
#endif
1124+
}
11151125
in = PyBytes_AsString(s);
11161126
size = PyBytes_GET_SIZE(s);
11171127
str = PyBytes_FromStringAndSize((char *) NULL, size*2+3);
1118-
if (!str) return PyErr_NoMemory();
1128+
if (!str) {
1129+
Py_DECREF(s);
1130+
return PyErr_NoMemory();
1131+
}
11191132
out = PyBytes_AS_STRING(str);
11201133
#if MYSQL_VERSION_ID < 32321
11211134
len = mysql_escape_string(out+1, in, size);
@@ -1336,7 +1349,11 @@ _mysql_field_to_python(
13361349
if (rowitem) {
13371350
if (converter != Py_None) {
13381351
v = PyObject_CallFunction(converter,
1352+
#ifdef IS_PY3K
1353+
"y#",
1354+
#else
13391355
"s#",
1356+
#endif
13401357
rowitem,
13411358
(int)length);
13421359
} else {

0 commit comments

Comments
 (0)