Repository: sagemath/sage

Issue:
save_session() is broken when interactive
I've never used this before, so I don't know if it used to work, but:
```
$ sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 10.2, Release Date: 2023-12-03                    │
│ Using Python 3.12.0. Type "help()" for help.                       │
└────────────────────────────────────────────────────────────────────┘
sage: save_session("misc")
<ipython-input-1-3181853d66d8>:1: DeprecationWarning: 
Importing Euler_Phi from here is deprecated; please use "from sage.arith.misc import Euler_Phi" instead.
See https://github.com/sagemath/sage/issues/30322 for details.
  save_session("misc")
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[1], line 1
----> 1 save_session("misc")

File /usr/lib/python3.12/site-packages/sage/misc/session.pyx:313, in sage.misc.session.save_session (build/cythonized/sage/misc/session.c:4112)()
    311 # We iterate only over the new variables that were defined in this
    312 # session, since those are the only ones we will save.
--> 313 for k in show_identifiers(hidden = True):
    314     try:
    315         x = state[k]

File /usr/lib/python3.12/site-packages/sage/misc/session.pyx:232, in sage.misc.session.show_identifiers (build/cythonized/sage/misc/session.c:3769)()
    230     return val == DocTestTask.extra_globals.get(name, _none)
    231 
--> 232 return sorted([x for x, v in state.items() if _is_new_var(x, v, hidden)
    233                and not _in_extra_globals(x, v)])
    234 

RuntimeError: dictionary changed size during iteration
```

The curious thing is that when doing this as part of a doctest I don't get this failure.

btw, why should I get a deprecation warning for a variable I did not mean to use?


Discussion:

Resolving patch:
diff --git a/src/sage/doctest/forker.py b/src/sage/doctest/forker.py
--- a/src/sage/doctest/forker.py
+++ b/src/sage/doctest/forker.py
@@ -2477,19 +2477,6 @@ class DocTestTask():
         ['cputime', 'err', 'failures', 'optionals', 'tests', 'walltime', 'walltime_skips']
     """
 
-    extra_globals = {}
-    """
-    Extra objects to place in the global namespace in which tests are run.
-    Normally this should be empty but there are special cases where it may
-    be useful.
-
-    For example, in Sage versions 9.1 and earlier, on Python 3 add
-    ``long`` as an alias for ``int`` so that tests that use the
-    ``long`` built-in (of which there are many) still pass.  We did
-    this so that the test suite could run on Python 3 while Python 2
-    was still the default.
-    """
-
     def __init__(self, source):
         """
         Initialization.
@@ -2614,10 +2601,6 @@ def _run(self, runner, options, results):
         # Remove '__package__' item from the globals since it is not
         # always in the globals in an actual Sage session.
         dict_all.pop('__package__', None)
-
-        # Add any other special globals for testing purposes only
-        dict_all.update(self.extra_globals)
-
         sage_namespace = RecordingDict(dict_all)
         sage_namespace['__name__'] = '__main__'
         doctests, extras = self.source.create_doctests(sage_namespace)


Test patch: