Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
src/sage/__init__.py [Cygwin]: Move monkey-patch of sqlite to src/sag…
Browse files Browse the repository at this point in the history
…e/cpython/__init__.py
  • Loading branch information
Matthias Koeppe committed Sep 8, 2021
1 parent 2e9ea56 commit ad587cf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
39 changes: 0 additions & 39 deletions src/sage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
39 changes: 39 additions & 0 deletions src/sage/cpython/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ad587cf

Please sign in to comment.