Skip to content

bpo-42264: Deprecate sqlite3.OptimizedUnicode #23163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 17, 2020
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ Deprecated
as appropriate to help identify code which needs updating during
this transition.

* ``sqlite3.OptimizedUnicode`` has been deprecated. It has been undocumented
and obsolete since Python 3.3.
(Contributed by Erlend E. Aasland in :issue:`42264`.)


Removed
=======
Expand Down
3 changes: 2 additions & 1 deletion Lib/sqlite3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.

from sqlite3.dbapi2 import *
from .dbapi2 import *
from .deprecated import __getattr__
10 changes: 10 additions & 0 deletions Lib/sqlite3/deprecated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def __getattr__(name):
if name == "OptimizedUnicode":
import warnings
msg = ("""
OptimizedUnicode is obsolete. You can safely remove it from your
code, as it defaults to 'str' anyway.
""")
warnings.warn(msg, DeprecationWarning)
return str
raise AttributeError(f"module 'sqlite3' has no attribute '{name}'")
12 changes: 5 additions & 7 deletions Lib/sqlite3/test/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,13 @@ def CheckCustom(self):
self.assertTrue(row[0].endswith("reich"), "column must contain original data")

def CheckOptimizedUnicode(self):
# In py3k, str objects are always returned when text_factory
# is OptimizedUnicode
self.con.text_factory = sqlite.OptimizedUnicode
# OptimizedUnicode is deprecated as of Python 3.10
with self.assertWarns(DeprecationWarning):
self.con.text_factory = sqlite.OptimizedUnicode
austria = "�sterreich"
germany = "Deutchland"
a_row = self.con.execute("select ?", (austria,)).fetchone()
d_row = self.con.execute("select ?", (germany,)).fetchone()
self.assertEqual(type(a_row[0]), str, "type of non-ASCII row must be str")
self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str")
self.con.execute("select ?", (austria,)).fetchone()
self.con.execute("select ?", (germany,)).fetchone()

def tearDown(self):
self.con.close()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate ``sqlite3.OptimizedUnicode``
9 changes: 0 additions & 9 deletions Modules/_sqlite/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,6 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
ADD_EXCEPTION(module, "DataError", pysqlite_DataError, pysqlite_DatabaseError);
ADD_EXCEPTION(module, "NotSupportedError", pysqlite_NotSupportedError, pysqlite_DatabaseError);

/* In Python 2.x, setting Connection.text_factory to
OptimizedUnicode caused Unicode objects to be returned for
non-ASCII data and bytestrings to be returned for ASCII data.
Now OptimizedUnicode is an alias for str, so it has no
effect. */
if (PyModule_AddObjectRef(module, "OptimizedUnicode", (PyObject*)&PyUnicode_Type) < 0) {
goto error;
}

/* Set integer constants */
if (add_integer_constants(module) < 0) {
goto error;
Expand Down