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

Commit

Permalink
trac 16522: introduce debugging routine to access LazyImport attribut…
Browse files Browse the repository at this point in the history
…es and a routine to clean namespaces of misconfigured LazyImport objects
  • Loading branch information
nbruin authored and Matthias Koeppe committed Dec 18, 2022
1 parent bb7ee85 commit a29a25d
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/sage/misc/lazy_import.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,65 @@ def get_star_imports(module_name):
star_imports[module_name] = all
return all

def attributes(a):
"""
Returns the private attributes of a LazyImport object in a dictionary (for debugging purposes).
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):
"""
Adjusts LazyImport bindings in given namespace to refer to this actual namespace.
When LazyImport objects are imported into other namespaces via normal import
instructions, then the data stored on a 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.
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
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
Expand Down

0 comments on commit a29a25d

Please sign in to comment.