Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoeppe committed Dec 18, 2022
2 parents 5789608 + 9837dec commit 7f1da65
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/sage/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ def quit_sage(verbose=True):
set_random_seed()


# Relink imported lazy_import objects to point to the appropriate namespace

from sage.misc.lazy_import import clean_namespace
clean_namespace()
del clean_namespace

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

Expand Down
40 changes: 20 additions & 20 deletions src/sage/combinat/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5813,26 +5813,7 @@ def __classcall_private__(cls, n=None, **kwargs):
"""
if n == infinity:
raise ValueError("n cannot be infinite")
if n is None or n is NN or n is NonNegativeIntegers():
if len(kwargs) > 0:
if len(kwargs) == 1:
if 'max_part' in kwargs:
return Partitions_all_bounded(kwargs['max_part'])
if 'regular' in kwargs:
return RegularPartitions_all(kwargs['regular'])
if 'restricted' in kwargs:
return RestrictedPartitions_all(kwargs['restricted'])
elif len(kwargs) == 2:
if 'regular' in kwargs:
if kwargs['regular'] < 1 or kwargs['regular'] not in ZZ:
raise ValueError("the regularity must be a positive integer")
if 'max_part' in kwargs:
return RegularPartitions_bounded(kwargs['regular'], kwargs['max_part'])
if 'max_length' in kwargs:
return RegularPartitions_truncated(kwargs['regular'], kwargs['max_length'])
raise ValueError("the size must be specified with any keyword argument")
return Partitions_all()
elif isinstance(n, (int,Integer)):
if isinstance(n, (int,Integer)):
if len(kwargs) == 0:
return Partitions_n(n)

Expand Down Expand Up @@ -5886,6 +5867,25 @@ def __classcall_private__(cls, n=None, **kwargs):
kwargs.get('min_length',0))
del kwargs['inner']
return Partitions_with_constraints(n, **kwargs)
elif n is None or n is NN or n is NonNegativeIntegers():
if len(kwargs) > 0:
if len(kwargs) == 1:
if 'max_part' in kwargs:
return Partitions_all_bounded(kwargs['max_part'])
if 'regular' in kwargs:
return RegularPartitions_all(kwargs['regular'])
if 'restricted' in kwargs:
return RestrictedPartitions_all(kwargs['restricted'])
elif len(kwargs) == 2:
if 'regular' in kwargs:
if kwargs['regular'] < 1 or kwargs['regular'] not in ZZ:
raise ValueError("the regularity must be a positive integer")
if 'max_part' in kwargs:
return RegularPartitions_bounded(kwargs['regular'], kwargs['max_part'])
if 'max_length' in kwargs:
return RegularPartitions_truncated(kwargs['regular'], kwargs['max_length'])
raise ValueError("the size must be specified with any keyword argument")
return Partitions_all()

raise ValueError("n must be an integer or be equal to one of "
"None, NN, NonNegativeIntegers()")
Expand Down
72 changes: 72 additions & 0 deletions src/sage/misc/lazy_import.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,78 @@ def get_star_imports(module_name):
return all


def attributes(a):
"""
Return the private attributes of a :class:`LazyImport` object in a dictionary.
This is for debugging and doctesting purposes only.
EXAMPLES::
sage: from sage.misc.lazy_import import attributes
sage: lazy_import("sage.all", "foo")
sage: attributes(foo)['_namespace'] is globals()
True
sage: D = attributes(foo)
sage: del D['_namespace']
sage: D
{'_as_name': 'foo',
'_at_startup': False,
'_deprecation': None,
'_module': 'sage.all',
'_name': 'foo',
'_object': None}
"""
cdef LazyImport b
b = a
return {"_object": b._object,
"_module": b._module,
"_name": b._name,
"_as_name": b._as_name,
"_namespace": b._namespace,
"_at_startup": b._at_startup,
"_deprecation": b._deprecation}


def clean_namespace(namespace=None):
"""
Adjust :class:`LazyImport` bindings in given namespace to refer to this actual namespace.
When :class:`LazyImport` objects are imported into other namespaces via normal ``import``
instructions, the data stored on a :class:`LazyImport` object that helps it to adjust the
binding in the namespace to the actual imported object upon access is not adjusted.
This routine fixes that.
INPUT:
- ``namespace`` -- the namespace where importing the names; by default,
import the names to current namespace
EXAMPLES::
sage: from sage.misc.lazy_import import attributes, clean_namespace
sage: from sage.calculus.calculus import maxima as C
sage: attributes(C)['_as_name']
'maxima'
sage: attributes(C)['_namespace'] is sage.calculus.calculus.__dict__
True
sage: clean_namespace(globals())
sage: attributes(C)['_as_name']
'C'
sage: attributes(C)['_namespace'] is globals()
True
"""
cdef LazyImport w
if namespace is None:
namespace = inspect.currentframe().f_locals
for k, v in namespace.items():
if type(v) is LazyImport:
w = v
if w._namespace is not None and (w._namespace is not namespace or w._as_name != k):
namespace[k] = LazyImport(w._module, w._name, as_name=k, at_startup=w._at_startup,
namespace=namespace, deprecation=w._deprecation)


# Add support for _instancedoc_
from sage.misc.instancedoc import instancedoc
instancedoc(LazyImport)
2 changes: 2 additions & 0 deletions src/sage/repl/user_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def initialize_globals(all, g=None):
for key in dir(all):
if key[0] != '_':
user_globals[key] = getattr(all, key)
from sage.misc.lazy_import import clean_namespace
clean_namespace(user_globals)


def get_global(name):
Expand Down

0 comments on commit 7f1da65

Please sign in to comment.