diff --git a/src/sage/__init__.py b/src/sage/__init__.py index b8d5315b946..49098fa7e08 100644 --- a/src/sage/__init__.py +++ b/src/sage/__init__.py @@ -1,14 +1,7 @@ -__all__ = ['all'] +# Do not add anything to this file. +# It will be removed soon in order to turn 'sage' into a native namespace package. +# See https://trac.sagemath.org/ticket/29705 -# Set sage.__version__ to the current version number. This is analogous -# to many other Python packages. -from sage.version import version as __version__ - -import sys -# Make sure that the correct zlib library is loaded. This is needed -# to prevent the system zlib to be loaded instead of the Sage one. -# See https://trac.sagemath.org/ticket/23122 -import zlib # IPython calls this when starting up def load_ipython_extension(*args): @@ -44,51 +37,3 @@ def isfunction(obj): deprecation(32479, "sage.isfunction is deprecated; use callable or sage.misc.sageinspect.is_function_or_cython_function instead") from sage.misc.sageinspect import is_function_or_cython_function return is_function_or_cython_function(obj) - - -# Monkey-patch ExtensionFileLoader to allow IPython to find the sources -# of Cython files. See https://trac.sagemath.org/ticket/24681 -try: - from importlib.machinery import ExtensionFileLoader -except ImportError: - pass # Python 2 -else: - 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 -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 e69de29bb2d..19c8d87a3be 100644 --- a/src/sage/cpython/__init__.py +++ b/src/sage/cpython/__init__.py @@ -0,0 +1,55 @@ +# sage.cpython is an ordinary package, not a namespace package. + +# This package is imported very early, which is why workarounds/monkey-patching +# are done in this file. + +# Make sure that the correct zlib library is loaded. This is needed +# to prevent the system zlib to be loaded instead of the Sage one. +# See https://trac.sagemath.org/ticket/23122 +import zlib as _zlib +del _zlib + +# Monkey-patch ExtensionFileLoader to allow IPython to find the sources +# of Cython files. See https://trac.sagemath.org/ticket/24681 +from importlib.machinery import ExtensionFileLoader as _ExtensionFileLoader +del _ExtensionFileLoader.get_source +del _ExtensionFileLoader + +# 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 + del _sys