diff --git a/pymbolic/mapper/stringifier.py b/pymbolic/mapper/stringifier.py index ce6f0b8eacbc55b06ac77d5e43b6f3cedf2b9cc7..c5abb3a827b4f6b28709bdd327676750ebef1650 100644 --- a/pymbolic/mapper/stringifier.py +++ b/pymbolic/mapper/stringifier.py @@ -119,13 +119,13 @@ class StringifyMapper(pymbolic.mapper.Mapper): # {{{ mappings - def handle_unsupported_expression(self, victim, enclosing_prec, *args, **kwargs): - strifier = victim.stringifier() - if isinstance(self, strifier): + def handle_unsupported_expression(self, expr, enclosing_prec, *args, **kwargs): + strifier = expr.make_stringifier(self) + if isinstance(self, type(strifier)): raise ValueError("stringifier '%s' can't handle '%s'" - % (self, victim.__class__)) - return strifier(self.constant_mapper)( - victim, enclosing_prec, *args, **kwargs) + % (self, expr.__class__)) + return strifier( + expr, enclosing_prec, *args, **kwargs) def map_constant(self, expr, enclosing_prec, *args, **kwargs): result = self.constant_mapper(expr) diff --git a/pymbolic/primitives.py b/pymbolic/primitives.py index 8bc804249a427014faedbd5a0a48779ad6b699ef..fbead08a579bea7dd1c7967e546aab0757a0b38a 100644 --- a/pymbolic/primitives.py +++ b/pymbolic/primitives.py @@ -191,7 +191,7 @@ class Expression(object): .. method:: __getitem__ - .. automethod:: stringifier + .. automethod:: make_stringifier .. automethod:: __eq__ .. automethod:: __hash__ @@ -446,13 +446,30 @@ class Expression(object): from pymbolic.mapper.evaluator import evaluate_to_float return evaluate_to_float(self) - def stringifier(self): - """Return a :class:`pymbolic.mapper.Mapper` class used to yield - a human-readable representation of *self*. Usually a subclass - of :class:`pymbolic.mapper.stringifier.StringifyMapper`. + def make_stringifier(self, originating_stringifier=None): + """Return a :class:`pymbolic.mapper.Mapper` instance that can + be used to generate a human-readable representation of *self*. Usually + a subclass of :class:`pymbolic.mapper.stringifier.StringifyMapper`. """ + if originating_stringifier is None: + stringify_mapper_args = () + else: + stringify_mapper_args = originating_stringifier.constant_mapper + + try: + stringifier_class_getter = self.stringifier + except AttributeError: + pass + else: + from warnings import warn + warn("%s overrides 'stringifier', which is deprecated. " + "Override 'make_stringifier' instead.") + + if stringifier_class_getter is not None: + return stringifier_class_getter()(*stringify_mapper_args) + from pymbolic.mapper.stringifier import StringifyMapper - return StringifyMapper + return StringifyMapper(*stringify_mapper_args) def __str__(self): """Use the :meth:`stringifier` to return a human-readable @@ -460,7 +477,7 @@ class Expression(object): """ from pymbolic.mapper.stringifier import PREC_NONE - return self.stringifier()()(self, PREC_NONE) + return self.make_stringifier()(self, PREC_NONE) def _safe_repr(self, limit=10): if limit <= 0: diff --git a/pymbolic/version.py b/pymbolic/version.py index 922dd503cf5856279cfaadc1adedf45efb8b35cd..6ba2863610f2fe534ddbfabbbd4a41673be4294c 100644 --- a/pymbolic/version.py +++ b/pymbolic/version.py @@ -1,3 +1,3 @@ -VERSION = (2018, 1) +VERSION = (2019, 1) VERSION_STATUS = "" VERSION_TEXT = ".".join(str(x) for x in VERSION) + VERSION_STATUS