--- a/branches/MySQLdb-1.2/MySQLdb/_mysql.c
+++ b/branches/MySQLdb-1.2/MySQLdb/_mysql.c
@@ -26,12 +26,15 @@
 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 @@
 	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 @@
 		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 @@
 		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 @@
 		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 @@
 		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 @@
 					 ))
 		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 @@
 	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 @@
 {
 	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 @@
 	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 @@
 	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 @@
 	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 @@
 		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 @@
 	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 @@
 						  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 @@
 #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 @@
 	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 @@
 		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 @@
 {
 	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 @@
 {
 	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 @@
 {
 	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 @@
 	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 @@
 	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 @@
 	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__[] =