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

Commit

Permalink
Merge branch 'public/build/startupWarning' of git://trac.sagemath.org…
Browse files Browse the repository at this point in the history
…/sage into public/refactoring/pytest
  • Loading branch information
tobiasdiez committed Nov 7, 2020
2 parents 79abf0d + b952bb5 commit 5de08cd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 70 deletions.
4 changes: 0 additions & 4 deletions src/sage/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,6 @@ def _write_started_file():
# Sage startup).
set_random_seed()


# From now on it is ok to resolve lazy imports
sage.misc.lazy_import.finish_startup()

def sage_globals():
r"""
Return the Sage namespace.
Expand Down
9 changes: 5 additions & 4 deletions src/sage/all_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

sage_mode = 'cmdline'

from sage.all import *
from sage.calculus.predefined import x

sage.misc.session.init()
import sage.misc.startup_guard as startup_guard

with startup_guard.startup():
from sage.all import *
from sage.calculus.predefined import x
sage.misc.session.init()
65 changes: 3 additions & 62 deletions src/sage/misc/lazy_import.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ AUTHOR:
cimport cython
from cpython.object cimport PyObject_RichCompare
from cpython.number cimport PyNumber_TrueDivide, PyNumber_Power, PyNumber_Index
import sage.misc.startup_guard as startup_guard

cdef extern from *:
int likely(int) nogil # Defined by Cython
Expand All @@ -76,66 +77,6 @@ cdef inline obj(x):
else:
return x


# boolean to determine whether Sage is still starting up
cdef bint startup_guard = True


cpdef finish_startup():
"""
This function must be called exactly once at the end of the Sage
import process
TESTS::
sage: from sage.misc.lazy_import import finish_startup
sage: finish_startup()
Traceback (most recent call last):
...
AssertionError: finish_startup() must be called exactly once
"""
global startup_guard
assert startup_guard, 'finish_startup() must be called exactly once'
startup_guard = False

cpdef bint is_during_startup():
"""
Return whether Sage is currently starting up.
OUTPUT:
Boolean
TESTS::
sage: from sage.misc.lazy_import import is_during_startup
sage: is_during_startup()
False
"""
global startup_guard
return startup_guard

cpdef test_fake_startup():
"""
For testing purposes only.
Switch the startup lazy import guard back on.
EXAMPLES::
sage: sage.misc.lazy_import.test_fake_startup()
sage: from sage.misc.lazy_import import lazy_import
sage: lazy_import('sage.rings.all', 'ZZ', 'my_ZZ')
sage: my_ZZ(123)
Traceback (most recent call last):
...
RuntimeError: resolving lazy import ZZ during startup
sage: sage.misc.lazy_import.finish_startup()
"""
global startup_guard
startup_guard = True


@cython.final
cdef class LazyImport(object):
"""
Expand Down Expand Up @@ -216,9 +157,9 @@ cdef class LazyImport(object):
if self._object is not None:
return self._object

if startup_guard and not self._at_startup:
if startup_guard.IS_STARTUP and not self._at_startup:
raise RuntimeError(f"resolving lazy import {self._name} during startup")
elif self._at_startup and not startup_guard:
elif self._at_startup and not startup_guard.IS_STARTUP:
print('Option ``at_startup=True`` for lazy import {0} not needed anymore'.format(self._name))
try:
self._object = getattr(__import__(self._module, {}, {}, [self._name]), self._name)
Expand Down
25 changes: 25 additions & 0 deletions src/sage/misc/startup_guard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from contextlib import contextmanager

IS_STARTUP: bool = False

@contextmanager
def startup():
"""
Simple context manager to indicate whether Sage is currently starting,
e.g. importing `sage.all` etc.
EXAMPLES::
sage: import sage.misc.startup_guard as startup_guard
sage: print(startup_guard.IS_STARTUP)
False
sage: with startup_guard.startup():
sage: print(startup_guard.IS_STARTUP)
True
sage: print(startup_guard.IS_STARTUP)
False
"""
global IS_STARTUP
IS_STARTUP = True
yield
IS_STARTUP = False

0 comments on commit 5de08cd

Please sign in to comment.