Ticket #11040: 11040-lazy-schemes.patch

File 11040-lazy-schemes.patch, 6.0 KB (added by robertwb, 11 years ago)
  • sage/all.py

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1267868560 28800
    # Node ID 5e2f50b4e8c9e7d23b74ff8d6ae3701bc314f9ce
    # Parent  361a4ad7d52c69b64ae2e658ffd0820af0d87e93
    #11040 - lazily import schemes for better startup time
    
    diff -r 361a4ad7d52c -r 5e2f50b4e8c9 sage/all.py
    a b  
    8080from sage.modules.all    import *
    8181from sage.monoids.all    import *
    8282from sage.algebras.all   import *
    83 from sage.modular.all    import *
    84 from sage.schemes.all    import *
     83lazy_import("sage.modular.all", "*", overwrite=False)
     84lazy_import("sage.schemes.all", "*", overwrite=False)
    8585from sage.graphs.all     import *
    8686from sage.groups.all     import *
    87 from sage.databases.all  import *
     87lazy_import("sage.databases.all", "*", overwrite=False)
    8888from sage.structure.all  import *
    8989from sage.categories.all import *
    9090from sage.sets.all       import *
     
    108108from sage.coding.all     import *
    109109from sage.combinat.all   import *
    110110
    111 from sage.lfunctions.all import *
     111lazy_import("sage.lfunctions.all", "*", overwrite=False)
    112112
    113113from sage.geometry.all   import *
    114114from sage.geometry.triangulation.all   import *
     
    152152#after a few releases, and this code should be removed.
    153153#--Mike Hansen 9/25/2008
    154154message = "\nUsing %(name)s from the top level is deprecated since it was designed to be used by developers rather than end users.\nIt most likely does not do what you would expect it to do.  If you really need to use it, import it from the module that it is defined in."
     155from sage.misc.lazy_import import LazyImport
    155156sage.misc.misc.deprecated_callable_import(None, globals(), locals(),
    156                                           [name for name in globals().keys()
    157                                            if name.startswith('is_') and name[3].isupper()], message)
     157                                          [name for name, value in globals().items()
     158                                           if name.startswith('is_') and name[3].isupper() and type(value) is not LazyImport], message)
    158159
    159160del message, name
    160161
     
    176177#except:
    177178#    pass
    178179
    179 # very useful 2-letter shortcuts
    180 CC = ComplexField()
    181 QQ = RationalField()
    182 RR = RealField()  # default real field
    183 ZZ = IntegerRing()
    184180# NOTE: QQ, RR, and ZZ are used by the pre-parser, and should not be
    185181# overwritten by the user, unless they want to change the meaning of
    186182# int and real in the interpreter (which is a potentially valid thing
     
    315311#sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', '', )
    316312
    317313# Cache the contents of star imports.
    318 import sage.misc.lazy_import
    319314sage.misc.lazy_import.save_cache_file()
     315sage.misc.lazy_import._sage_all_import_done = True
  • sage/coding/code_constructions.py

    diff -r 361a4ad7d52c -r 5e2f50b4e8c9 sage/coding/code_constructions.py
    a b  
    142142
    143143
    144144import copy
     145from sage.misc.lazy_import import lazy_import
    145146import sage.modules.free_module as fm
    146147import sage.modules.module as module
    147148import sage.modules.free_module_element as fme
     
    158159from linear_code import LinearCodeFromVectorSpace, LinearCode
    159160from sage.modules.free_module import span
    160161from sage.misc.functional import rank
    161 from sage.schemes.generic.projective_space import ProjectiveSpace
    162162from sage.structure.sequence import Sequence
    163163from sage.rings.arith import GCD,LCM,divisors,quadratic_residues
    164164from sage.rings.finite_rings.integer_mod_ring import IntegerModRing
     
    166166from sage.rings.integer import Integer
    167167from sage.sets.set import Set
    168168from sage.rings.finite_rings.integer_mod import Mod
     169lazy_import('sage.schemes.generic.projective_space', 'ProjectiveSpace')
    169170
    170171############### utility functions ################
    171172
  • sage/misc/lazy_import.pyx

    diff -r 361a4ad7d52c -r 5e2f50b4e8c9 sage/misc/lazy_import.pyx
    a b  
    791791        sage: 'EllipticCurve' in get_star_imports('sage.schemes.all')
    792792        True
    793793    """
    794     global star_imports
     794    global star_imports, _sage_all_import_done
    795795    if star_imports is None:
    796796        cache_file = get_cache_file()
    797797        if os.path.exists(cache_file):
     
    801801    try:
    802802        return star_imports[module_name]
    803803    except KeyError:
     804        all_done = _sage_all_import_done
     805        _sage_all_import_done = True # so that checks pass
    804806        module = __import__(module_name, {}, {}, ["*"])
     807        _sage_all_import_done = all_done
    805808        if hasattr(module, "__all__"):
    806809            all = module.__all__
    807810        else:
    808811            all = [key for key in dir(module) if key[0] != "_"]
    809812        star_imports[module_name] = all
    810813        return all
     814
     815# Utility functions for asserting that things stay lazily imported.
     816
     817_sage_all_import_done = False
     818
     819def check_lazy(module_name = None):
     820    """
     821    Used to assert lazily imported modules don't actually get imported
     822    during the import of sage.all is done.
     823   
     824    TESTS::
     825   
     826        sage: from sage.misc.lazy_import import check_lazy
     827        sage: check_lazy("foo")
     828        True
     829        sage: sage.misc.lazy_import._sage_all_import_done = False
     830        sage: check_lazy("foo")
     831        False
     832        sage: sage.misc.lazy_import._sage_all_import_done = True
     833        sage: check_lazy("foo")
     834        True
     835    """
     836    if module_name is None:
     837        module_name = inspect.currentframe().f_globals['__name__']
     838    return _sage_all_import_done
  • sage/schemes/all.py

    diff -r 361a4ad7d52c -r 5e2f50b4e8c9 sage/schemes/all.py
    a b  
    2020#                  http://www.gnu.org/licenses/
    2121#*****************************************************************************
    2222
     23import sage.misc.lazy_import
     24assert sage.misc.lazy_import.check_lazy()
     25
    2326from jacobians.all import *
    2427
    2528from hyperelliptic_curves.all import *