diff -r cf70f030a744 Lib/sqlite3/dbapi2.py --- a/Lib/sqlite3/dbapi2.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/sqlite3/dbapi2.py Mon Jun 23 22:10:09 2014 +0300 @@ -70,7 +70,7 @@ timepart_full = timepart.split(".") hours, minutes, seconds = map(int, timepart_full[0].split(":")) if len(timepart_full) == 2: - microseconds = int('{:0<6.6}'.format(timepart_full[1].decode())) + microseconds = int('{:0<6.6}'.format(timepart_full[1])) else: microseconds = 0 diff -r cf70f030a744 Lib/sqlite3/test/factory.py --- a/Lib/sqlite3/test/factory.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/sqlite3/test/factory.py Mon Jun 23 22:10:09 2014 +0300 @@ -24,6 +24,12 @@ import unittest import sqlite3 as sqlite from collections import Sequence +from test.test_support import have_unicode, requires_unicode + +if have_unicode: + _str = unicode +else: + _str = str class MyConnection(sqlite.Connection): def __init__(self, *args, **kwargs): @@ -173,6 +179,7 @@ def tearDown(self): self.con.close() +@requires_unicode class TextFactoryTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") @@ -217,7 +224,7 @@ def CheckString(self): # text_factory defaults to unicode row = self.con.execute("select value from test").fetchone() - self.assertIs(type(row[0]), unicode) + self.assertIs(type(row[0]), _str) self.assertEqual(row[0], "a\x00b") def CheckCustom(self): @@ -234,6 +241,7 @@ self.assertIs(type(row[0]), str) self.assertEqual(row[0], "a\x00b") + @requires_unicode def CheckOptimizedUnicodeAsUnicode(self): # Non-ASCII -> unicode argument self.con.text_factory = sqlite.OptimizedUnicode diff -r cf70f030a744 Lib/sqlite3/test/regression.py --- a/Lib/sqlite3/test/regression.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/sqlite3/test/regression.py Mon Jun 23 22:10:09 2014 +0300 @@ -24,6 +24,7 @@ import datetime import unittest import sqlite3 as sqlite +from test.test_support import requires_unicode class RegressionTests(unittest.TestCase): def setUp(self): @@ -116,6 +117,7 @@ """ self.con.execute("") + @requires_unicode def CheckUnicodeConnect(self): """ With pysqlite 2.4.0 you needed to use a string or a APSW connection @@ -149,6 +151,7 @@ """ self.assertRaises(TypeError, sqlite.register_adapter, {}, None) + @requires_unicode def CheckSetIsolationLevel(self): """ See issue 3312. diff -r cf70f030a744 Lib/sqlite3/test/types.py --- a/Lib/sqlite3/test/types.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/sqlite3/test/types.py Mon Jun 23 22:10:09 2014 +0300 @@ -28,6 +28,7 @@ import zlib except ImportError: zlib = None +from test.test_support import have_unicode, requires_unicode class SqliteTypeTests(unittest.TestCase): @@ -73,11 +74,13 @@ row = self.cur.fetchone() self.assertEqual(row[0], val) + @requires_unicode def CheckUnicodeExecute(self): self.cur.execute(u"select 'Österreich'") row = self.cur.fetchone() self.assertEqual(row[0], u"Österreich") + @requires_unicode def CheckNonUtf8_Default(self): try: self.cur.execute("select ?", (chr(150),)) @@ -192,6 +195,7 @@ row = self.cur.fetchone() self.assertEqual(row[0], True) + @requires_unicode def CheckUnicode(self): # default val = u"\xd6sterreich" diff -r cf70f030a744 Lib/sqlite3/test/userfunctions.py --- a/Lib/sqlite3/test/userfunctions.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/sqlite3/test/userfunctions.py Mon Jun 23 22:10:09 2014 +0300 @@ -24,11 +24,18 @@ import unittest import sqlite3 as sqlite +from test.test_support import have_unicode, requires_unicode + +if have_unicode: + _str = unicode +else: + _str = str def func_returntext(): return "foo" -def func_returnunicode(): - return u"bar" +if have_unicode: + def func_returnunicode(): + return u"bar" def func_returnint(): return 42 def func_returnfloat(): @@ -43,7 +50,7 @@ 5 // 0 def func_isstring(v): - return type(v) is unicode + return type(v) is _str def func_isint(v): return type(v) is int def func_isfloat(v): @@ -104,7 +111,7 @@ self.val = None def step(self, whichType, val): - theType = {"str": unicode, "int": int, "float": float, "None": type(None), "blob": buffer} + theType = {"str": _str, "int": int, "float": float, "None": type(None), "blob": buffer} self.val = int(theType[whichType] is type(val)) def finalize(self): @@ -125,7 +132,8 @@ self.con = sqlite.connect(":memory:") self.con.create_function("returntext", 0, func_returntext) - self.con.create_function("returnunicode", 0, func_returnunicode) + if have_unicode: + self.con.create_function("returnunicode", 0, func_returnunicode) self.con.create_function("returnint", 0, func_returnint) self.con.create_function("returnfloat", 0, func_returnfloat) self.con.create_function("returnnull", 0, func_returnnull) @@ -166,9 +174,10 @@ cur = self.con.cursor() cur.execute("select returntext()") val = cur.fetchone()[0] - self.assertEqual(type(val), unicode) + self.assertEqual(type(val), _str) self.assertEqual(val, "foo") + @requires_unicode def CheckFuncReturnUnicode(self): cur = self.con.cursor() cur.execute("select returnunicode()") diff -r cf70f030a744 Modules/_sqlite/connection.c --- a/Modules/_sqlite/connection.c Wed Jun 18 23:07:46 2014 -0400 +++ b/Modules/_sqlite/connection.c Mon Jun 23 22:10:09 2014 +0300 @@ -91,18 +91,30 @@ Py_INCREF(Py_None); self->row_factory = Py_None; +#ifdef Py_USING_UNICODE Py_INCREF(&PyUnicode_Type); self->text_factory = (PyObject*)&PyUnicode_Type; +#else + Py_INCREF(&PyString_Type); + self->text_factory = (PyObject*)&PyString_Type; +#endif - if (PyString_Check(database) || PyUnicode_Check(database)) { - if (PyString_Check(database)) { - database_utf8 = database; - Py_INCREF(database_utf8); - } else { + if (PyString_Check(database) +#ifdef Py_USING_UNICODE + || PyUnicode_Check(database) +#endif + ) { +#ifdef Py_USING_UNICODE + if (PyUnicode_Check(database)) { database_utf8 = PyUnicode_AsUTF8String(database); if (!database_utf8) { return -1; } + } else +#endif + { + database_utf8 = database; + Py_INCREF(database_utf8); } Py_BEGIN_ALLOW_THREADS @@ -564,12 +576,14 @@ sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT); } else if (PyString_Check(py_val)) { sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT); +#ifdef Py_USING_UNICODE } else if (PyUnicode_Check(py_val)) { PyObject * stringval = PyUnicode_AsUTF8String(py_val); if (!stringval) return -1; sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT); Py_DECREF(stringval); +#endif } else { return -1; } @@ -602,7 +616,11 @@ break; case SQLITE_TEXT: val_str = (const char*)sqlite3_value_text(cur_value); +#ifdef Py_USING_UNICODE cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL); +#else + cur_py_value = PyString_FromString(val_str); +#endif /* TODO: have a way to show errors here */ if (!cur_py_value) { PyErr_Clear(); diff -r cf70f030a744 Modules/_sqlite/cursor.c --- a/Modules/_sqlite/cursor.c Wed Jun 18 23:07:46 2014 -0400 +++ b/Modules/_sqlite/cursor.c Mon Jun 23 22:10:09 2014 +0300 @@ -261,6 +261,7 @@ PyObject* pysqlite_unicode_from_string(const char* val_str, Py_ssize_t size, int optimize) { +#ifdef Py_USING_UNICODE const char* check; Py_ssize_t pos; int is_ascii = 0; @@ -279,11 +280,11 @@ } } - if (is_ascii) { + if (!is_ascii) + return PyUnicode_DecodeUTF8(val_str, size, NULL); + else +#endif return PyString_FromStringAndSize(val_str, size); - } else { - return PyUnicode_DecodeUTF8(val_str, size, NULL); - } } /* @@ -362,8 +363,11 @@ } else if (coltype == SQLITE_TEXT) { val_str = (const char*)sqlite3_column_text(self->statement->st, i); nbytes = sqlite3_column_bytes(self->statement->st, i); - if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type) - || (self->connection->text_factory == pysqlite_OptimizedUnicode)) { + if ( +#ifdef Py_USING_UNICODE + (self->connection->text_factory == (PyObject*)&PyUnicode_Type) || +#endif + (self->connection->text_factory == pysqlite_OptimizedUnicode)) { converted = pysqlite_unicode_from_string(val_str, nbytes, self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0); @@ -377,7 +381,8 @@ colname , val_str); PyErr_SetString(pysqlite_OperationalError, buf); } - } else if (self->connection->text_factory == (PyObject*)&PyString_Type) { + } else + if (self->connection->text_factory == (PyObject*)&PyString_Type) { converted = PyString_FromStringAndSize(val_str, nbytes); } else { converted = PyObject_CallFunction(self->connection->text_factory, "s#", val_str, nbytes); @@ -464,7 +469,10 @@ self->reset = 0; /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */ - allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) && + allow_8bit_chars = ( +#ifdef Py_USING_UNICODE + (self->connection->text_factory != (PyObject*)&PyUnicode_Type) && +#endif (self->connection->text_factory != pysqlite_OptimizedUnicode)); Py_CLEAR(self->next_row); @@ -475,7 +483,11 @@ goto error; } - if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { + if (!PyString_Check(operation) +#ifdef Py_USING_UNICODE + && !PyUnicode_Check(operation) +#endif + ) { PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); goto error; } @@ -497,7 +509,11 @@ goto error; } - if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { + if (!PyString_Check(operation) +#ifdef Py_USING_UNICODE + && !PyUnicode_Check(operation) +#endif + ) { PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); goto error; } @@ -532,16 +548,17 @@ rc = pysqlite_statement_reset(self->statement); } - if (PyString_Check(operation)) { - operation_cstr = PyString_AsString(operation); - } else { +#ifdef Py_USING_UNICODE + if (PyUnicode_Check(operation)) { operation_bytestr = PyUnicode_AsUTF8String(operation); if (!operation_bytestr) { goto error; } operation_cstr = PyString_AsString(operation_bytestr); - } + } else +#endif + operation_cstr = PyString_AsString(operation); /* reset description and rowcount */ Py_DECREF(self->description); @@ -803,6 +820,7 @@ if (PyString_Check(script_obj)) { script_cstr = PyString_AsString(script_obj); +#ifdef Py_USING_UNICODE } else if (PyUnicode_Check(script_obj)) { script_str = PyUnicode_AsUTF8String(script_obj); if (!script_str) { @@ -810,6 +828,7 @@ } script_cstr = PyString_AsString(script_str); +#endif } else { PyErr_SetString(PyExc_ValueError, "script argument must be unicode or string."); return NULL; diff -r cf70f030a744 Modules/_sqlite/module.c --- a/Modules/_sqlite/module.c Wed Jun 18 23:07:46 2014 -0400 +++ b/Modules/_sqlite/module.c Mon Jun 23 22:10:09 2014 +0300 @@ -156,7 +156,11 @@ /* a basic type is adapted; there's a performance optimization if that's not the case * (99 % of all usages) */ if (type == &PyInt_Type || type == &PyLong_Type || type == &PyFloat_Type - || type == &PyString_Type || type == &PyUnicode_Type || type == &PyBuffer_Type) { + || type == &PyString_Type || type == &PyBuffer_Type +#ifdef Py_USING_UNICODE + || type == &PyUnicode_Type +#endif + ) { pysqlite_BaseTypeAdapted = 1; } diff -r cf70f030a744 Modules/_sqlite/statement.c --- a/Modules/_sqlite/statement.c Wed Jun 18 23:07:46 2014 -0400 +++ b/Modules/_sqlite/statement.c Mon Jun 23 22:10:09 2014 +0300 @@ -46,7 +46,9 @@ TYPE_LONG, TYPE_FLOAT, TYPE_STRING, +#ifdef Py_USING_UNICODE TYPE_UNICODE, +#endif TYPE_BUFFER, TYPE_UNKNOWN } parameter_type; @@ -64,12 +66,14 @@ if (PyString_Check(sql)) { sql_str = sql; Py_INCREF(sql_str); +#ifdef Py_USING_UNICODE } else if (PyUnicode_Check(sql)) { sql_str = PyUnicode_AsUTF8String(sql); if (!sql_str) { rc = PYSQLITE_SQL_WRONG_TYPE; return rc; } +#endif } else { rc = PYSQLITE_SQL_WRONG_TYPE; return rc; @@ -122,8 +126,10 @@ paramtype = TYPE_FLOAT; } else if (PyString_CheckExact(parameter)) { paramtype = TYPE_STRING; +#ifdef Py_USING_UNICODE } else if (PyUnicode_CheckExact(parameter)) { paramtype = TYPE_UNICODE; +#endif } else if (PyBuffer_Check(parameter)) { paramtype = TYPE_BUFFER; } else if (PyInt_Check(parameter)) { @@ -134,8 +140,10 @@ paramtype = TYPE_FLOAT; } else if (PyString_Check(parameter)) { paramtype = TYPE_STRING; +#ifdef Py_USING_UNICODE } else if (PyUnicode_Check(parameter)) { paramtype = TYPE_UNICODE; +#endif } else { paramtype = TYPE_UNKNOWN; } @@ -172,12 +180,14 @@ PyString_AsStringAndSize(parameter, &string, &buflen); rc = sqlite3_bind_text(self->st, pos, string, buflen, SQLITE_TRANSIENT); break; +#ifdef Py_USING_UNICODE case TYPE_UNICODE: stringval = PyUnicode_AsUTF8String(parameter); PyString_AsStringAndSize(stringval, &string, &buflen); rc = sqlite3_bind_text(self->st, pos, string, buflen, SQLITE_TRANSIENT); Py_DECREF(stringval); break; +#endif case TYPE_BUFFER: if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) { rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT); @@ -203,7 +213,11 @@ if (PyInt_CheckExact(obj) || PyLong_CheckExact(obj) || PyFloat_CheckExact(obj) || PyString_CheckExact(obj) - || PyUnicode_CheckExact(obj) || PyBuffer_Check(obj)) { + || PyBuffer_Check(obj) +#ifdef Py_USING_UNICODE + || PyUnicode_CheckExact(obj) +#endif + ) { return 0; } else { return 1;