diff --git a/src/sage/__init__.py b/src/sage/__init__.py index c9ab41a32ad..a5fc2015841 100644 --- a/src/sage/__init__.py +++ b/src/sage/__init__.py @@ -2,7 +2,6 @@ # It will be removed soon in order to turn 'sage' into a native namespace package. # See https://trac.sagemath.org/ticket/29705 -import sys # IPython calls this when starting up def load_ipython_extension(*args): @@ -51,41 +50,3 @@ def isfunction(obj): import inspect inspect.isfunction = isfunction - - -# Work around a Cygwin-specific bug caused by sqlite3; see -# https://trac.sagemath.org/ticket/30157 and the docstring for -# fix_for_ticket_30157 -# Here we monkey-patch the sqlite3 module to ensure the fix is -# applied the very first time a connection is made to a sqlite3 -# database -if sys.platform == 'cygwin': - def patch_sqlite3(): - try: - from sage.misc.sage_ostools import fix_for_ticket_30157 - except ImportError: - # The module might not have been re-built yet; don't worry about it - # then - return - - import sqlite3 - import functools - orig_sqlite3_connect = sqlite3.connect - - @functools.wraps(orig_sqlite3_connect) - def connect(*args, **kwargs): - if fix_for_ticket_30157(): - raise RuntimeError( - 'patch for Trac ticket #30157 failed; please report this ' - 'bug to https://trac.sagemath.org') - - # Undo the monkey-patch - try: - return orig_sqlite3_connect(*args, **kwargs) - finally: - sqlite3.connect = orig_sqlite3_connect - - sqlite3.connect = connect - - patch_sqlite3() - del patch_sqlite3 diff --git a/src/sage/cpython/__init__.py b/src/sage/cpython/__init__.py index e372ebdd5bc..c158a73f1fa 100644 --- a/src/sage/cpython/__init__.py +++ b/src/sage/cpython/__init__.py @@ -13,3 +13,42 @@ # of Cython files. See https://trac.sagemath.org/ticket/24681 from importlib.machinery import ExtensionFileLoader as _ExtensionFileLoader del _ExtensionFileLoader.get_source + + +# Work around a Cygwin-specific bug caused by sqlite3; see +# https://trac.sagemath.org/ticket/30157 and the docstring for +# fix_for_ticket_30157 +# Here we monkey-patch the sqlite3 module to ensure the fix is +# applied the very first time a connection is made to a sqlite3 +# database +import sys as _sys +if _sys.platform == 'cygwin': + def _patch_sqlite3(): + try: + from sage.misc.sage_ostools import fix_for_ticket_30157 + except ImportError: + # The module might not have been re-built yet; don't worry about it + # then + return + + import sqlite3 + import functools + orig_sqlite3_connect = sqlite3.connect + + @functools.wraps(orig_sqlite3_connect) + def connect(*args, **kwargs): + if fix_for_ticket_30157(): + raise RuntimeError( + 'patch for Trac ticket #30157 failed; please report this ' + 'bug to https://trac.sagemath.org') + + # Undo the monkey-patch + try: + return orig_sqlite3_connect(*args, **kwargs) + finally: + sqlite3.connect = orig_sqlite3_connect + + sqlite3.connect = connect + + _patch_sqlite3() + del _patch_sqlite3