sagemath/sage

Remove some monkey patching in src/sage/__init__.py

Closed this issue · 27 comments

To make sage a namespace package, we need to get rid of src/sage/__init__.py.

It currently contains:

  • definition of __all__ (no longer needed; has no effect on completion, neither in plain python3 nor IPython)
  • definition of __version__ (unused)
  • preloading of zlib (introduced in #23122)
  • #32489: load_ipython_extension (introduced in #18726): This function is defined in module sage so that %load_ext sage works (see http://www.slabbe.org/blogue/categorie/ipython/); we forgot to define the corresponding function that would make %unload_ext sage work
  • #32479: monkey patching of inspect.isfunction to support Cython functions (introduced in #25373)
  • monkey patching of importlib.machinery.ExtensionFileLoader (introduced in #24681); the quest to get an upstream fix merged was apparently abandoned - python/cpython#6653
  • monkey patching of sqlite on Cygwin (introduced in #30157)

In this ticket we remove the ones that are not needed any more, and some others to another module init. #32479 and #32489 will take care of the leftovers.

CC: @kiwifb @jhpalmieri @embray

Component: refactoring

Author: Matthias Koeppe

Branch/Commit: af6642f

Reviewer: François Bissey

Issue created by migration from https://trac.sagemath.org/ticket/31420

Description changed:

--- 
+++ 
@@ -3,7 +3,7 @@
 It currently contains:
 - definition of `__all__`
 - definition of `__version__`
-- preloading of zlib
+- preloading of `zlib`
 - `load_ipython_extension`
 - monkey patching of `inspect.isfunction` to support Cython functions
 - monkey patching of sqlite on Cygwin
comment:2

Sage development has entered the release candidate phase for 9.3. Setting a new milestone for this ticket based on a cursory review of ticket status, priority, and last modification date.

Description changed:

--- 
+++ 
@@ -5,7 +5,7 @@
 - definition of `__version__`
 - preloading of `zlib`
 - `load_ipython_extension`
-- monkey patching of `inspect.isfunction` to support Cython functions
+- #32479: monkey patching of `inspect.isfunction` to support Cython functions
 - monkey patching of sqlite on Cygwin
 
 We should review all of them, remove anything that is not needed any more, and move the rest to other module inits if possible.

Description changed:

--- 
+++ 
@@ -3,10 +3,11 @@
 It currently contains:
 - definition of `__all__`
 - definition of `__version__`
-- preloading of `zlib`
-- `load_ipython_extension`
-- #32479: monkey patching of `inspect.isfunction` to support Cython functions
-- monkey patching of sqlite on Cygwin
+- preloading of `zlib` (introduced in #23122)
+- `load_ipython_extension` (introduced in #18726)
+- #32479: monkey patching of `inspect.isfunction` to support Cython functions (introduced in #25373)
+- monkey patching of `importlib.machinery.ExtensionFileLoader` (introduced in #24681)
+- monkey patching of sqlite on Cygwin (introduced in #30157)
 
 We should review all of them, remove anything that is not needed any more, and move the rest to other module inits if possible.
 

Description changed:

--- 
+++ 
@@ -4,7 +4,7 @@
 - definition of `__all__`
 - definition of `__version__`
 - preloading of `zlib` (introduced in #23122)
-- `load_ipython_extension` (introduced in #18726)
+- `load_ipython_extension` (introduced in #18726): This function is defined in module `sage` so that `%load_ext sage` works (see http://www.slabbe.org/blogue/categorie/ipython/); we forgot to define the corresponding method that makes `%unload_ext sage` work
 - #32479: monkey patching of `inspect.isfunction` to support Cython functions (introduced in #25373)
 - monkey patching of `importlib.machinery.ExtensionFileLoader` (introduced in #24681)
 - monkey patching of sqlite on Cygwin (introduced in #30157)

Description changed:

--- 
+++ 
@@ -4,7 +4,7 @@
 - definition of `__all__`
 - definition of `__version__`
 - preloading of `zlib` (introduced in #23122)
-- `load_ipython_extension` (introduced in #18726): This function is defined in module `sage` so that `%load_ext sage` works (see http://www.slabbe.org/blogue/categorie/ipython/); we forgot to define the corresponding method that makes `%unload_ext sage` work
+- `load_ipython_extension` (introduced in #18726): This function is defined in module `sage` so that `%load_ext sage` works (see http://www.slabbe.org/blogue/categorie/ipython/); we forgot to define the corresponding function that would make `%unload_ext sage` work
 - #32479: monkey patching of `inspect.isfunction` to support Cython functions (introduced in #25373)
 - monkey patching of `importlib.machinery.ExtensionFileLoader` (introduced in #24681)
 - monkey patching of sqlite on Cygwin (introduced in #30157)

Description changed:

--- 
+++ 
@@ -6,7 +6,7 @@
 - preloading of `zlib` (introduced in #23122)
 - `load_ipython_extension` (introduced in #18726): This function is defined in module `sage` so that `%load_ext sage` works (see http://www.slabbe.org/blogue/categorie/ipython/); we forgot to define the corresponding function that would make `%unload_ext sage` work
 - #32479: monkey patching of `inspect.isfunction` to support Cython functions (introduced in #25373)
-- monkey patching of `importlib.machinery.ExtensionFileLoader` (introduced in #24681)
+- monkey patching of `importlib.machinery.ExtensionFileLoader` (introduced in #24681); the quest to get an upstream fix merged was apparently abandoned - https://github.com/python/cpython/pull/6653
 - monkey patching of sqlite on Cygwin (introduced in #30157)
 
 We should review all of them, remove anything that is not needed any more, and move the rest to other module inits if possible.

Description changed:

--- 
+++ 
@@ -2,7 +2,7 @@
 
 It currently contains:
 - definition of `__all__`
-- definition of `__version__`
+- definition of `__version__` (unused)
 - preloading of `zlib` (introduced in #23122)
 - `load_ipython_extension` (introduced in #18726): This function is defined in module `sage` so that `%load_ext sage` works (see http://www.slabbe.org/blogue/categorie/ipython/); we forgot to define the corresponding function that would make `%unload_ext sage` work
 - #32479: monkey patching of `inspect.isfunction` to support Cython functions (introduced in #25373)

Description changed:

--- 
+++ 
@@ -1,7 +1,7 @@
 To make `sage` a namespace package, we need to get rid of `src/sage/__init__.py`.
 
 It currently contains:
-- definition of `__all__`
+- definition of `__all__` (no longer needed; has no effect on completion, neither in plain python3 nor IPython)
 - definition of `__version__` (unused)
 - preloading of `zlib` (introduced in #23122)
 - `load_ipython_extension` (introduced in #18726): This function is defined in module `sage` so that `%load_ext sage` works (see http://www.slabbe.org/blogue/categorie/ipython/); we forgot to define the corresponding function that would make `%unload_ext sage` work

Description changed:

--- 
+++ 
@@ -4,7 +4,7 @@
 - definition of `__all__` (no longer needed; has no effect on completion, neither in plain python3 nor IPython)
 - definition of `__version__` (unused)
 - preloading of `zlib` (introduced in #23122)
-- `load_ipython_extension` (introduced in #18726): This function is defined in module `sage` so that `%load_ext sage` works (see http://www.slabbe.org/blogue/categorie/ipython/); we forgot to define the corresponding function that would make `%unload_ext sage` work
+- #32489: `load_ipython_extension` (introduced in #18726): This function is defined in module `sage` so that `%load_ext sage` works (see http://www.slabbe.org/blogue/categorie/ipython/); we forgot to define the corresponding function that would make `%unload_ext sage` work
 - #32479: monkey patching of `inspect.isfunction` to support Cython functions (introduced in #25373)
 - monkey patching of `importlib.machinery.ExtensionFileLoader` (introduced in #24681); the quest to get an upstream fix merged was apparently abandoned - https://github.com/python/cpython/pull/6653
 - monkey patching of sqlite on Cygwin (introduced in #30157)
comment:12

It seems that removing __version__ and __all__ is harmless. I don't know about the zlib issue: I tried moving the import to all.py and everything worked, but that's hardly an exhaustive test.

comment:13

We can probably move it (and also the sqlite monkey-patching on cygwin) to a module that is loaded very early such as sage.cpython.__init__.

Commit: ad587cf

comment:15

Do you also want del _sys?


New commits:

91ec0f2src/sage/__init__.py: Remove __all__
949d787src/sage/__init__.py: Remove __version__
f77a98esrc/sage/__init__.py: Move 'import zlib' to src/sage/cpython/__init__.py
2e9ea56src/sage/__init__.py: Move monkey-patch of ExtensionFileLoader to src/sage/cpython/__init__.py
ad587cfsrc/sage/__init__.py [Cygwin]: Move monkey-patch of sqlite to src/sage/cpython/__init__.py

Changed commit from ad587cf to af6642f

Branch pushed to git repo; I updated commit sha1. New commits:

af6642fsrc/sage/cpython/__init__.py: Clean up sage.cpython module

Author: Matthias Koeppe

Description changed:

--- 
+++ 
@@ -9,5 +9,6 @@
 - monkey patching of `importlib.machinery.ExtensionFileLoader` (introduced in #24681); the quest to get an upstream fix merged was apparently abandoned - https://github.com/python/cpython/pull/6653
 - monkey patching of sqlite on Cygwin (introduced in #30157)
 
-We should review all of them, remove anything that is not needed any more, and move the rest to other module inits if possible.
+In this ticket we remove the ones that are not needed any more, and some others to another module init. #32479 and #32489 will take care of the leftovers.
 
+
comment:19

Where does the del _zlib come from in sage/cpython/__init__.py? It wasn't present in sage/__init__.py and the ticket above that group only talks about import zlib. Is mentioning that ticket obsolete? Is the whole block obsolete when we favor system zlib whenever possible?

comment:20

The del introduced in af6642f just removes the binding of the name _zlib created by the import statement from the sage.cpython module. Likewise the other dels in this commit.

comment:21

Call me obtuse, why do you import and then delete straight away? Is it a cleaner way to make sure that the right library is loaded while you may not want it around in this particular unit?

comment:22

The importing there is just to trigger loading some shared library.
One could use a function from importlib instead of using import but that's a tiny bit more obscure

comment:23

Thank you for taking the time to answer my questions. It looks good to go.

Reviewer: François Bissey

comment:24

Thanks for reviewing!