From 9c0dc68fdbeae38ff0c2e86e16e7a313fe42585c Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Fri, 3 Oct 2014 16:19:28 -0500 Subject: [PATCH] Bump version, document disable_subscript_by_getitem --- doc/Makefile | 2 +- doc/_static/akdoc.css | 8 ++++---- doc/_templates/layout.html | 2 +- doc/conf.py | 2 +- pymbolic/__init__.py | 2 +- pymbolic/mapper/__init__.py | 2 +- pymbolic/primitives.py | 38 ++++++++++++++++++++++++++++++++++++- pymbolic/version.py | 2 +- test/test_pymbolic.py | 5 +++++ 9 files changed, 52 insertions(+), 11 deletions(-) diff --git a/doc/Makefile b/doc/Makefile index 668e7c8..4da073e 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -3,7 +3,7 @@ # You can set these variables from the command line. SPHINXOPTS = -SPHINXBUILD = sphinx-build +SPHINXBUILD = python `which sphinx-build` PAPER = BUILDDIR = _build diff --git a/doc/_static/akdoc.css b/doc/_static/akdoc.css index e83c3b7..d8b61e3 100644 --- a/doc/_static/akdoc.css +++ b/doc/_static/akdoc.css @@ -1,5 +1,5 @@ pre { - line-height: 100%; + line-height: 110%; } .footer { @@ -23,17 +23,17 @@ code { } h1 { - padding-bottom:5px; + padding-bottom:7px; border-bottom: 1px solid #ccc; } h2 { - padding-bottom:1px; + padding-bottom:5px; border-bottom: 1px solid #ccc; } h3 { - padding-bottom:1px; + padding-bottom:5px; border-bottom: 1px solid #ccc; } diff --git a/doc/_templates/layout.html b/doc/_templates/layout.html index 0fed238..400e7ec 100644 --- a/doc/_templates/layout.html +++ b/doc/_templates/layout.html @@ -1,2 +1,2 @@ {% extends "!layout.html" %} -{% set css_files = css_files + ['_static/akdoc.css']%} +{% set bootswatch_css_custom = ['_static/akdoc.css']%} diff --git a/doc/conf.py b/doc/conf.py index c2dfa23..d57aa79 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -31,7 +31,7 @@ extensions = [ 'sphinx.ext.intersphinx', 'sphinx.ext.coverage', 'sphinx.ext.mathjax', - 'sphinx.ext.viewcode' + #'sphinx.ext.viewcode' ] # Add any paths that contain templates here, relative to this directory. diff --git a/pymbolic/__init__.py b/pymbolic/__init__.py index 4f1101f..740febc 100644 --- a/pymbolic/__init__.py +++ b/pymbolic/__init__.py @@ -134,7 +134,7 @@ import pymbolic.mapper.distributor import pymbolic.mapper.flattener import pymbolic.primitives -from pymbolic.polynomial import Polynomial +from pymbolic.polynomial import Polynomial # noqa var = pymbolic.primitives.Variable variables = pymbolic.primitives.variables diff --git a/pymbolic/mapper/__init__.py b/pymbolic/mapper/__init__.py index 8f7c6ae..e0c0d23 100644 --- a/pymbolic/mapper/__init__.py +++ b/pymbolic/mapper/__init__.py @@ -334,7 +334,7 @@ class IdentityMapper(Mapper): ) def map_subscript(self, expr, *args): - return expr.__class__( + return type(expr)( self.rec(expr.aggregate, *args), self.rec(expr.index, *args)) diff --git a/pymbolic/primitives.py b/pymbolic/primitives.py index 84ce880..74727a5 100644 --- a/pymbolic/primitives.py +++ b/pymbolic/primitives.py @@ -25,6 +25,8 @@ THE SOFTWARE. import pymbolic.traits as traits __doc__ = """ +.. autofunction:: disable_subscript_by_getitem + Expression base class --------------------- @@ -37,6 +39,13 @@ Expression base class The :class:`pymbolic.mapper.Mapper` method called for objects of this type. + .. method:: __getitem__ + + Deprecated, see :func:`disable_subscript_by_getitem`. Use :meth:`index` + instead. + + .. automethod:: index + .. automethod:: stringifier .. automethod:: __eq__ @@ -178,10 +187,32 @@ _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 - del Expression.__getitem__ + try: + del Expression.__getitem__ + except AttributeError: + # Yay, somebody did the sane thing before us. + pass class Expression(object): @@ -395,6 +426,11 @@ class Expression(object): return NotImplemented def index(self, subscript): + """Return an expression representing ``self[subscript]``. + + .. versionadded:: 2014.3 + """ + if subscript == (): return self else: diff --git a/pymbolic/version.py b/pymbolic/version.py index 5849b39..e98c37c 100644 --- a/pymbolic/version.py +++ b/pymbolic/version.py @@ -1,3 +1,3 @@ -VERSION = (2014, 2) +VERSION = (2014, 3) VERSION_STATUS = "" VERSION_TEXT = ".".join(str(x) for x in VERSION) + VERSION_STATUS diff --git a/test/test_pymbolic.py b/test/test_pymbolic.py index e5c32d0..48c4740 100644 --- a/test/test_pymbolic.py +++ b/test/test_pymbolic.py @@ -22,9 +22,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +import pymbolic import pymbolic.primitives as prim import pytest + from pymbolic.mapper import IdentityMapper try: @@ -33,6 +35,8 @@ except NameError: from functools import reduce +pymbolic.disable_subscript_by_getitem() + def test_integer_power(): from pymbolic.algorithm import integer_power @@ -250,6 +254,7 @@ def test_mappers(): WalkMapper()(expr) DependencyMapper()(expr) + # {{{ geometric algebra @pytest.mark.parametrize("dims", [2, 3, 4, 5]) -- GitLab