Skip to content

Commit

Permalink
Backport PR ipython#14029: Allow safe access to the `__getattribute__…
Browse files Browse the repository at this point in the history
…` method of modules
  • Loading branch information
Carreau authored and meeseeksmachine committed May 2, 2023
1 parent a09873d commit 4049037
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions IPython/core/guarded_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ def _list_methods(cls, source=None):

dict_keys: Type[collections.abc.KeysView] = type({}.keys())
method_descriptor: Any = type(list.copy)
module = type(builtins)

NUMERICS = {int, float, complex}

Expand Down Expand Up @@ -686,6 +687,7 @@ def _list_methods(cls, source=None):
*NUMERICS,
dict_keys,
method_descriptor,
module,
}


Expand Down
12 changes: 12 additions & 0 deletions IPython/core/tests/test_guarded_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,15 @@ class B(NamedTuple):
pass

assert A.__getitem__ == B.__getitem__


@dec.skip_without("numpy")
def test_module_access():
import numpy

context = limited(numpy=numpy)
assert guarded_eval("numpy.linalg.norm", context) == numpy.linalg.norm

context = minimal(numpy=numpy)
with pytest.raises(GuardRejection):
guarded_eval("np.linalg.norm", context)

0 comments on commit 4049037

Please sign in to comment.