diff --git a/pymbolic/primitives.py b/pymbolic/primitives.py index f9caa48248f8fa579c5e378d582651548bfcda72..a23c25413b613078d4d15b0ab4a7f9e5d97563a9 100644 --- a/pymbolic/primitives.py +++ b/pymbolic/primitives.py @@ -31,8 +31,6 @@ from six.moves import range from six.moves import zip __doc__ = """ -.. autofunction:: disable_subscript_by_getitem - Expression base class --------------------- @@ -189,36 +187,14 @@ vectors and matrices of :mod:`pymbolic` objects. """ -_SUBSCRIPT_BY_GETITEM = True - - def disable_subscript_by_getitem(): - """In prior versions of :mod:`pymbolic`, directly subscripting an :class:`Expression` - subclass generated a :class:`Subscript`. For various reasons, this was a - very bad idea. For example, the following code snippet would result in an - infinite loop:: - - for el in expr: - print(el) - - :mod:`numpy` does similar things under the hodd, leading to hard-to-debug - infinite loops. As a result, this behavior is being deprecated. In Pymbolic - 2016.x, it will disappear entirely. It can also be disabled by this - function. Once disabled, it cannot be reenabled. - - See also :meth:`Expression.index`. - - .. versionadded:: 2014.3 - """ - - global _SUBSCRIPT_BY_GETITEM - _SUBSCRIPT_BY_GETITEM = False - - try: - del Expression.__getitem__ - except AttributeError: - # Yay, somebody did the sane thing before us. - pass + # The issue that was addressed by this could be fixed + # in a much less ham-fisted manner, and thus this has been + # made a no-op. + # + # See + # https://github.com/inducer/pymbolic/issues/4 + pass class Expression(object): @@ -415,22 +391,6 @@ class Expression(object): else: return Call(self, args) - def __getitem__(self, subscript): - if _SUBSCRIPT_BY_GETITEM: - from warnings import warn - - warn("creating subscripts using x[i] syntax is deprecated " - "and will be removed in Pymbolic 2016.x. " - "Use x.index(i) instead.", - DeprecationWarning, stacklevel=2) - - if subscript == (): - return self - else: - return Subscript(self, subscript) - else: - return NotImplemented - def index(self, subscript): """Return an expression representing ``self[subscript]``. @@ -442,6 +402,8 @@ class Expression(object): else: return Subscript(self, subscript) + __getitem__ = index + def attr(self, name): """Return a :class:`Lookup` for *name* in *self*. """ @@ -547,6 +509,10 @@ class Expression(object): # }}} + def __iter__(self): + # prevent infinite loops (e.g. when inseserting into numpy arrays) + raise TypeError("expression types are not iterable") + class AlgebraicLeaf(Expression): """An expression that serves as a leaf for arithmetic evaluation.