Skip to content

Commit a1f401a

Browse files
author
Erlend Egeberg Aasland
authored
bpo-42264: Deprecate sqlite3.OptimizedUnicode (GH-23163)
1 parent 296a796 commit a1f401a

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

Doc/whatsnew/3.10.rst

+5
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ Deprecated
360360
as appropriate to help identify code which needs updating during
361361
this transition.
362362

363+
* ``sqlite3.OptimizedUnicode`` has been undocumented and obsolete since Python
364+
3.3, when it was made an alias to :class:`str`. It is now deprecated,
365+
scheduled for removal in Python 3.12.
366+
(Contributed by Erlend E. Aasland in :issue:`42264`.)
367+
363368

364369
Removed
365370
=======

Lib/sqlite3/__init__.py

+14
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@
2121
# 3. This notice may not be removed or altered from any source distribution.
2222

2323
from sqlite3.dbapi2 import *
24+
25+
26+
# bpo-42264: OptimizedUnicode was deprecated in Python 3.10. It's scheduled
27+
# for removal in Python 3.12.
28+
def __getattr__(name):
29+
if name == "OptimizedUnicode":
30+
import warnings
31+
msg = ("""
32+
OptimizedUnicode is deprecated and will be removed in Python 3.12.
33+
Since Python 3.3 it has simply been an alias for 'str'.
34+
""")
35+
warnings.warn(msg, DeprecationWarning, stacklevel=2)
36+
return str
37+
raise AttributeError(f"module 'sqlite3' has no attribute '{name}'")

Lib/sqlite3/test/factory.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,10 @@ def CheckCustom(self):
254254
self.assertTrue(row[0].endswith("reich"), "column must contain original data")
255255

256256
def CheckOptimizedUnicode(self):
257-
# In py3k, str objects are always returned when text_factory
258-
# is OptimizedUnicode
259-
self.con.text_factory = sqlite.OptimizedUnicode
257+
# OptimizedUnicode is deprecated as of Python 3.10
258+
with self.assertWarns(DeprecationWarning) as cm:
259+
self.con.text_factory = sqlite.OptimizedUnicode
260+
self.assertIn("factory.py", cm.filename)
260261
austria = "Österreich"
261262
germany = "Deutchland"
262263
a_row = self.con.execute("select ?", (austria,)).fetchone()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``sqlite3.OptimizedUnicode`` has been undocumented and obsolete since Python
2+
3.3, when it was made an alias to :class:`str`. It is now deprecated,
3+
scheduled for removal in Python 3.12.

Modules/_sqlite/module.c

-9
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,6 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
412412
ADD_EXCEPTION(module, "DataError", pysqlite_DataError, pysqlite_DatabaseError);
413413
ADD_EXCEPTION(module, "NotSupportedError", pysqlite_NotSupportedError, pysqlite_DatabaseError);
414414

415-
/* In Python 2.x, setting Connection.text_factory to
416-
OptimizedUnicode caused Unicode objects to be returned for
417-
non-ASCII data and bytestrings to be returned for ASCII data.
418-
Now OptimizedUnicode is an alias for str, so it has no
419-
effect. */
420-
if (PyModule_AddObjectRef(module, "OptimizedUnicode", (PyObject*)&PyUnicode_Type) < 0) {
421-
goto error;
422-
}
423-
424415
/* Set integer constants */
425416
if (add_integer_constants(module) < 0) {
426417
goto error;

0 commit comments

Comments
 (0)