From af2c7e58384b6e3a41117a0467ae85055052a882 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 4 Nov 2020 20:54:21 +0100 Subject: [PATCH 01/11] Deprecate OptimizedUnicode --- Lib/sqlite3/__init__.py | 3 ++- Lib/sqlite3/deprecated.py | 10 ++++++++++ Lib/sqlite3/test/factory.py | 12 +++++------- Modules/_sqlite/module.c | 9 --------- 4 files changed, 17 insertions(+), 17 deletions(-) create mode 100644 Lib/sqlite3/deprecated.py diff --git a/Lib/sqlite3/__init__.py b/Lib/sqlite3/__init__.py index 6c91df27cca70d..8df381b8c3bc5d 100644 --- a/Lib/sqlite3/__init__.py +++ b/Lib/sqlite3/__init__.py @@ -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__ diff --git a/Lib/sqlite3/deprecated.py b/Lib/sqlite3/deprecated.py new file mode 100644 index 00000000000000..6fec7745bb040b --- /dev/null +++ b/Lib/sqlite3/deprecated.py @@ -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}'") diff --git a/Lib/sqlite3/test/factory.py b/Lib/sqlite3/test/factory.py index 95dd24bdfadca6..f6c97b89212af4 100644 --- a/Lib/sqlite3/test/factory.py +++ b/Lib/sqlite3/test/factory.py @@ -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() diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 9fdf51417ed883..372f3dda4cbee4 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -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; From f8dfb6dd7a1b7d1fb1c4b93ba9db440fc45b674a Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 5 Nov 2020 13:39:14 +0100 Subject: [PATCH 02/11] Add NEWS and update "What's new" --- Doc/whatsnew/3.10.rst | 4 ++++ .../next/Library/2020-11-05-13-32-41.bpo-42264.r4KYUU.rst | 1 + 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-11-05-13-32-41.bpo-42264.r4KYUU.rst diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 9d9284897be8ab..2cce3ace6f60fc 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -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 ======= 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 index 00000000000000..4d1677c48f1326 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-11-05-13-32-41.bpo-42264.r4KYUU.rst @@ -0,0 +1 @@ +Deprecate ``sqlite3.OptimizedUnicode`` From f25e85ab6402da2d355c35e73216a3e2666f92ab Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 5 Nov 2020 13:41:26 +0100 Subject: [PATCH 03/11] Restore type checks in unit test --- Lib/sqlite3/test/factory.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/sqlite3/test/factory.py b/Lib/sqlite3/test/factory.py index f6c97b89212af4..f12f236c462838 100644 --- a/Lib/sqlite3/test/factory.py +++ b/Lib/sqlite3/test/factory.py @@ -259,8 +259,10 @@ def CheckOptimizedUnicode(self): self.con.text_factory = sqlite.OptimizedUnicode austria = "Österreich" germany = "Deutchland" - self.con.execute("select ?", (austria,)).fetchone() - self.con.execute("select ?", (germany,)).fetchone() + 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") def tearDown(self): self.con.close() From a9e959f63bfc2d5aec21ca57e713dd549411a139 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 5 Nov 2020 14:37:12 +0100 Subject: [PATCH 04/11] Address review comment: Refer to caller when warning, and verify this --- Lib/sqlite3/deprecated.py | 2 +- Lib/sqlite3/test/factory.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/sqlite3/deprecated.py b/Lib/sqlite3/deprecated.py index 6fec7745bb040b..ae94622e3164c5 100644 --- a/Lib/sqlite3/deprecated.py +++ b/Lib/sqlite3/deprecated.py @@ -5,6 +5,6 @@ def __getattr__(name): OptimizedUnicode is obsolete. You can safely remove it from your code, as it defaults to 'str' anyway. """) - warnings.warn(msg, DeprecationWarning) + warnings.warn(msg, DeprecationWarning, stacklevel=2) return str raise AttributeError(f"module 'sqlite3' has no attribute '{name}'") diff --git a/Lib/sqlite3/test/factory.py b/Lib/sqlite3/test/factory.py index f12f236c462838..d91997333b11ca 100644 --- a/Lib/sqlite3/test/factory.py +++ b/Lib/sqlite3/test/factory.py @@ -255,8 +255,9 @@ def CheckCustom(self): def CheckOptimizedUnicode(self): # OptimizedUnicode is deprecated as of Python 3.10 - with self.assertWarns(DeprecationWarning): + 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() From 3cd247fce884eef43e02754119a94f2bb3bbcb3e Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Thu, 5 Nov 2020 20:11:27 +0100 Subject: [PATCH 05/11] Address review: Improve NEWS text Co-authored-by: Victor Stinner --- .../next/Library/2020-11-05-13-32-41.bpo-42264.r4KYUU.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index 4d1677c48f1326..707d31f452ecb5 100644 --- 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 @@ -1 +1,2 @@ -Deprecate ``sqlite3.OptimizedUnicode`` +``sqlite3.OptimizedUnicode`` has been deprecated. It has been undocumented +and obsolete since Python 3.3. From b106c9ed7059ef950116c2bdea53ba3b61463b56 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 5 Nov 2020 20:42:46 +0100 Subject: [PATCH 06/11] Address review: improve "What's new" --- Doc/whatsnew/3.10.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 2cce3ace6f60fc..87e7d8ec6d0d6e 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -299,8 +299,9 @@ 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. +* ``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`.) From b364cf5d3be0208861e3fe05ee248d4491932282 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 5 Nov 2020 20:55:00 +0100 Subject: [PATCH 07/11] Address review: Move deprecation warning to __init__.py --- Lib/sqlite3/__init__.py | 15 ++++++++++++++- Lib/sqlite3/deprecated.py | 10 ---------- 2 files changed, 14 insertions(+), 11 deletions(-) delete mode 100644 Lib/sqlite3/deprecated.py diff --git a/Lib/sqlite3/__init__.py b/Lib/sqlite3/__init__.py index 8df381b8c3bc5d..e5d262e603f717 100644 --- a/Lib/sqlite3/__init__.py +++ b/Lib/sqlite3/__init__.py @@ -21,4 +21,17 @@ # 3. This notice may not be removed or altered from any source distribution. from .dbapi2 import * -from .deprecated import __getattr__ + + +# 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 obsolete. You can safely remove it from your + code, as it defaults to 'str' anyway. + """) + warnings.warn(msg, DeprecationWarning, stacklevel=2) + return str + raise AttributeError(f"module 'sqlite3' has no attribute '{name}'") diff --git a/Lib/sqlite3/deprecated.py b/Lib/sqlite3/deprecated.py deleted file mode 100644 index ae94622e3164c5..00000000000000 --- a/Lib/sqlite3/deprecated.py +++ /dev/null @@ -1,10 +0,0 @@ -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, stacklevel=2) - return str - raise AttributeError(f"module 'sqlite3' has no attribute '{name}'") From 3320b42eafce12611c02d2305856d60cbe7dde99 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 5 Nov 2020 21:00:20 +0100 Subject: [PATCH 08/11] Restore dbapi2 import in __init__.py --- Lib/sqlite3/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/sqlite3/__init__.py b/Lib/sqlite3/__init__.py index e5d262e603f717..6fc2784fb1f6ea 100644 --- a/Lib/sqlite3/__init__.py +++ b/Lib/sqlite3/__init__.py @@ -20,7 +20,7 @@ # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. -from .dbapi2 import * +from sqlite3.dbapi2 import * # OptimizedUnicode was deprecated in Python 3.10. It's scheduled for removal From 62d2e9b2f7fb4909c78cb57da078cb5094a91384 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Thu, 5 Nov 2020 23:19:50 +0100 Subject: [PATCH 09/11] Improve warning wording --- Lib/sqlite3/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/sqlite3/__init__.py b/Lib/sqlite3/__init__.py index 6fc2784fb1f6ea..0941aa80a30f29 100644 --- a/Lib/sqlite3/__init__.py +++ b/Lib/sqlite3/__init__.py @@ -29,8 +29,8 @@ 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. + 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 From 2507fd03301a47ee4df6eb45cbe99afbf322ab12 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 6 Nov 2020 20:08:20 +0100 Subject: [PATCH 10/11] Address review comment: Improve NEWS entry Use the same text as the What's New entry. --- .../next/Library/2020-11-05-13-32-41.bpo-42264.r4KYUU.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 index 707d31f452ecb5..dd8e6871eb8cd3 100644 --- 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 @@ -1,2 +1,3 @@ -``sqlite3.OptimizedUnicode`` has been deprecated. It has been undocumented -and obsolete since Python 3.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. From fe7c65e76213f9b4565ef3eba37008e60f2d834f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 6 Nov 2020 20:09:37 +0100 Subject: [PATCH 11/11] Address review comment: Add bpo reference to __init__.py Co-authored-by: Victor Stinner --- Lib/sqlite3/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/sqlite3/__init__.py b/Lib/sqlite3/__init__.py index 0941aa80a30f29..f001c0678e195f 100644 --- a/Lib/sqlite3/__init__.py +++ b/Lib/sqlite3/__init__.py @@ -23,8 +23,8 @@ from sqlite3.dbapi2 import * -# OptimizedUnicode was deprecated in Python 3.10. It's scheduled for removal -# in Python 3.12. +# 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