]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42264: Deprecate sqlite3.OptimizedUnicode (GH-23163)
authorErlend Egeberg Aasland <erlend.aasland@innova.no>
Tue, 17 Nov 2020 15:55:12 +0000 (16:55 +0100)
committerGitHub <noreply@github.com>
Tue, 17 Nov 2020 15:55:12 +0000 (17:55 +0200)
Doc/whatsnew/3.10.rst
Lib/sqlite3/__init__.py
Lib/sqlite3/test/factory.py
Misc/NEWS.d/next/Library/2020-11-05-13-32-41.bpo-42264.r4KYUU.rst [new file with mode: 0644]
Modules/_sqlite/module.c

index ffc34d773570f6c65a178904aa07274bdf6c9a12..786cc61003a593baae1cca61318d9981ac51726c 100644 (file)
@@ -360,6 +360,11 @@ Deprecated
   as appropriate to help identify code which needs updating during
   this transition.
 
+* ``sqlite3.OptimizedUnicode`` has been undocumented and obsolete since Python
+  3.3, when it was made an alias to :class:`str`.  It is now deprecated,
+  scheduled for removal in Python 3.12.
+  (Contributed by Erlend E. Aasland in :issue:`42264`.)
+
 
 Removed
 =======
index 6c91df27cca70d7fa397c41175d18c900b1747d9..f001c0678e195fae4624a2a296e0670521a3e1be 100644 (file)
 # 3. This notice may not be removed or altered from any source distribution.
 
 from sqlite3.dbapi2 import *
+
+
+# bpo-42264: OptimizedUnicode was deprecated in Python 3.10.  It's scheduled
+# for removal in Python 3.12.
+def __getattr__(name):
+    if name == "OptimizedUnicode":
+        import warnings
+        msg = ("""
+            OptimizedUnicode is deprecated and will be removed in Python 3.12.
+            Since Python 3.3 it has simply been an alias for 'str'.
+        """)
+        warnings.warn(msg, DeprecationWarning, stacklevel=2)
+        return str
+    raise AttributeError(f"module 'sqlite3' has no attribute '{name}'")
index 95dd24bdfadca694a81da0b56f4734aa46efa4a0..d91997333b11caf1ccb4cc0b5f3c9f92337b7600 100644 (file)
@@ -254,9 +254,10 @@ class TextFactoryTests(unittest.TestCase):
         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) as cm:
+            self.con.text_factory = sqlite.OptimizedUnicode
+        self.assertIn("factory.py", cm.filename)
         austria = "Österreich"
         germany = "Deutchland"
         a_row = self.con.execute("select ?", (austria,)).fetchone()
diff --git a/Misc/NEWS.d/next/Library/2020-11-05-13-32-41.bpo-42264.r4KYUU.rst b/Misc/NEWS.d/next/Library/2020-11-05-13-32-41.bpo-42264.r4KYUU.rst
new file mode 100644 (file)
index 0000000..dd8e687
--- /dev/null
@@ -0,0 +1,3 @@
+``sqlite3.OptimizedUnicode`` has been undocumented and obsolete since Python
+3.3, when it was made an alias to :class:`str`.  It is now deprecated,
+scheduled for removal in Python 3.12.
index 9fdf51417ed8830c3a1d1ba1dd88744cb67acfeb..372f3dda4cbee4394e35cc1fcef67bc340825e23 100644 (file)
@@ -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;