Skip to content

Commit ddf44d8

Browse files
author
Release Manager
committed
Trac #31306: sage.repl: Replace use of SAGE_EXTCODE by importlib.resources
We eliminate direct reading of files from the package directories of sagelib by using `importlib.resources` (available since python 3.7) - `git grep 'SAGE_EXTCODE' src/sage` - `git grep '__file__' src/sage` This will help make sagelib `zip_safe` (https://setuptools.readthedocs.i o/en/latest/userguide/miscellaneous.html#setting-the-zip-safe-flag) In this ticket, we take care of `sage.repl`, moving data needed by its doctests from `SAGE_EXTCODE` (= `src/sage/ext_data`) into the package directory. (Thus, we avoid a dependency on support for resources in namespace packages brought by Python 3.10(?) or the backport package `importlib-resources`, see python/importlib_resources#196) Follow-up tickets will deal with other parts of the library. In the end, the directory `src/sage/ext_data` will be eliminated. References: - https://docs.python.org/3/library/importlib.html#module- importlib.resources - https://importlib-resources.readthedocs.io/en/latest/migration.html - https://importlib-resources.readthedocs.io/en/latest/using.html URL: https://trac.sagemath.org/31306 Reported by: mkoeppe Ticket author(s): Matthias Koeppe Reviewer(s): Dima Pasechnik
2 parents d04fbee + d925cd2 commit ddf44d8

23 files changed

+27
-54
lines changed

src/sage/repl/display/formatter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,16 @@ def format(self, obj, include=None, exclude=None):
142142
TESTS::
143143
144144
sage: import os
145-
sage: from sage.env import SAGE_EXTCODE
146-
sage: example_png = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.png')
145+
sage: import importlib.resources
147146
sage: from sage.repl.rich_output.backend_ipython import BackendIPython
148147
sage: backend = BackendIPython()
149148
sage: shell = get_test_shell()
150149
sage: backend.install(shell=shell)
151150
sage: shell.run_cell('get_ipython().display_formatter')
152151
<sage.repl.display.formatter.SageDisplayFormatter object at 0x...>
153152
sage: shell.run_cell('from IPython.display import Image')
154-
sage: shell.run_cell('ipython_image = Image("{0}")'.format(example_png))
153+
sage: with importlib.resources.path(sage.repl.rich_output, 'example.png') as example_png:
154+
....: shell.run_cell('ipython_image = Image("{0}")'.format(example_png))
155155
sage: shell.run_cell('ipython_image')
156156
<IPython.core.display.Image object>
157157
sage: shell.run_cell('get_ipython().display_formatter.format(ipython_image)')

0 commit comments

Comments
 (0)