diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index a146249178f40d7..a60e5bbeb94ec9e 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -267,6 +267,9 @@ Deprecated versions. :class:`~ast.Constant` should be used instead. (Contributed by Serhiy Storchaka in :issue:`32892`.) +* :mod:`sqlite3` classes ``Cache`` and ``Statement`` + are considered deprecated and will be removed in future Python. + (Contributed by Aviv Palivoda in :issue:`30262`.) Removed ======= diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py index 34cd233535dc164..dac9c6785e575f1 100644 --- a/Lib/sqlite3/test/regression.py +++ b/Lib/sqlite3/test/regression.py @@ -398,9 +398,20 @@ def callback(*args): support.gc_collect() +class DeprecationTests(unittest.TestCase): + def CheckCacheDeprecationWarning(self): + with self.assertWarns(DeprecationWarning): + sqlite.Cache(int) + + def CheckStatementDeprecationWarning(self): + with self.assertWarns(DeprecationWarning): + sqlite.Statement() + + def suite(): regression_suite = unittest.makeSuite(RegressionTests, "Check") - return unittest.TestSuite((regression_suite,)) + deprecation_suite = unittest.makeSuite(DeprecationTests, "Check") + return unittest.TestSuite((regression_suite, deprecation_suite)) def test(): runner = unittest.TextTestRunner() diff --git a/Misc/NEWS.d/next/Library/2018-10-08-13-31-49.bpo-30262.g9P1x4.rst b/Misc/NEWS.d/next/Library/2018-10-08-13-31-49.bpo-30262.g9P1x4.rst new file mode 100644 index 000000000000000..1f17620252e897b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-10-08-13-31-49.bpo-30262.g9P1x4.rst @@ -0,0 +1,3 @@ + +Deprecate :mod:`sqlite3` ``Cache`` and ``Statement`` classes. +Patch by Aviv Palivoda diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index db2b4958b1463e6..e833728ffe98195 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -330,6 +330,69 @@ static const IntConstantPair _int_constants[] = { }; +static PyObject *pysqlite_cache_deprecated_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + pysqlite_Cache *self; + + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "Cache object has been deprecated, it won't be exposed in Python 3.9", + 1)) + return NULL; + + self = (pysqlite_Cache *) pysqlite_CacheType.tp_alloc(&pysqlite_CacheType, 0); + if (!self) { + return NULL; + } + + if (pysqlite_cache_init(self, args, kwargs) != 0) { + Py_TYPE(self)->tp_free((PyObject*)self); + return NULL; + } + return (PyObject *)self; +} + +static void pysqlite_deprecated_dealloc(PyObject* self) +{ + Py_TYPE(self)->tp_free((PyObject*)self); +} + + +static PyObject *pysqlite_statement_deprecated_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + pysqlite_Statement *self; + + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "Statement object has been deprecated, it won't be exposed in Python 3.9", + 1)) + return NULL; + + self = (pysqlite_Statement *) pysqlite_StatementType.tp_alloc(&pysqlite_StatementType, 0); + if (!self) { + return NULL; + } + + return (PyObject *)self; +} + + +static PyTypeObject pysqlite_CacheDeprecatedType = { + PyVarObject_HEAD_INIT(NULL, 0) + MODULE_NAME ".CacheDeprecated", /* tp_name */ + sizeof(pysqlite_CacheDeprecatedType), /* tp_basicsize */ + .tp_dealloc = (destructor)pysqlite_deprecated_dealloc, + .tp_new = pysqlite_cache_deprecated_new, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, +}; + +static PyTypeObject pysqlite_StatementDeprecatedType = { + PyVarObject_HEAD_INIT(NULL, 0) + MODULE_NAME ".StatementDeprecated", /* tp_name */ + sizeof(pysqlite_StatementDeprecatedType), /* tp_basicsize */ + .tp_dealloc = (destructor)pysqlite_deprecated_dealloc, + .tp_new = pysqlite_statement_deprecated_new, + .tp_flags = Py_TPFLAGS_DEFAULT, +}; + static struct PyModuleDef _sqlite3module = { PyModuleDef_HEAD_INIT, "_sqlite3", @@ -356,7 +419,9 @@ PyMODINIT_FUNC PyInit__sqlite3(void) (pysqlite_connection_setup_types() < 0) || (pysqlite_cache_setup_types() < 0) || (pysqlite_statement_setup_types() < 0) || - (pysqlite_prepare_protocol_setup_types() < 0) + (pysqlite_prepare_protocol_setup_types() < 0) || + (PyType_Ready(&pysqlite_CacheDeprecatedType) < 0) || + (PyType_Ready(&pysqlite_StatementDeprecatedType) < 0) ) { Py_XDECREF(module); return NULL; @@ -366,10 +431,10 @@ PyMODINIT_FUNC PyInit__sqlite3(void) PyModule_AddObject(module, "Connection", (PyObject*) &pysqlite_ConnectionType); Py_INCREF(&pysqlite_CursorType); PyModule_AddObject(module, "Cursor", (PyObject*) &pysqlite_CursorType); - Py_INCREF(&pysqlite_CacheType); - PyModule_AddObject(module, "Statement", (PyObject*)&pysqlite_StatementType); - Py_INCREF(&pysqlite_StatementType); - PyModule_AddObject(module, "Cache", (PyObject*) &pysqlite_CacheType); + Py_INCREF(&pysqlite_StatementDeprecatedType); + PyModule_AddObject(module, "Statement", (PyObject*) &pysqlite_StatementDeprecatedType); + Py_INCREF(&pysqlite_CacheDeprecatedType); + PyModule_AddObject(module, "Cache", (PyObject*) &pysqlite_CacheDeprecatedType); Py_INCREF(&pysqlite_PrepareProtocolType); PyModule_AddObject(module, "PrepareProtocol", (PyObject*) &pysqlite_PrepareProtocolType); Py_INCREF(&pysqlite_RowType);