From 1cf82b8bcbc92f7e4002d9a97914cead97087173 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 19 Dec 2020 23:53:04 -0800 Subject: [PATCH] Don't let module imports have side-effects. The pretty printer module was inscribing the trace command as a side- effect of importing it. --- joy/__main__.py | 6 +++--- joy/joy.py | 1 - joy/utils/pretty_print.py | 13 +++++-------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/joy/__main__.py b/joy/__main__.py index 29017c5..005473b 100644 --- a/joy/__main__.py +++ b/joy/__main__.py @@ -17,12 +17,12 @@ # You should have received a copy of the GNU General Public License # along with Thun. If not see . # -from __future__ import print_function -from .library import initialize +from .library import initialize, inscribe from .joy import repl -from .utils import pretty_print # Inscribe trace command. +from .utils.pretty_print import trace +inscribe(trace) print('''\ Thun - Copyright © 2017 Simon Forman This program comes with ABSOLUTELY NO WARRANTY; for details type "warranty". diff --git a/joy/joy.py b/joy/joy.py index d1e49b7..0e755da 100644 --- a/joy/joy.py +++ b/joy/joy.py @@ -23,7 +23,6 @@ attempts to stay very close to the spirit of Joy but does not precisely match the behaviour of the original version(s) written in C. ''' -from __future__ import print_function from builtins import input from traceback import print_exc from .parser import text_to_expression, ParseError, Symbol diff --git a/joy/utils/pretty_print.py b/joy/utils/pretty_print.py index 83e2877..3c6e8c5 100644 --- a/joy/utils/pretty_print.py +++ b/joy/utils/pretty_print.py @@ -38,15 +38,12 @@ the pending expression to the right. ''' # (Kinda clunky and hacky. This should be swapped out in favor of much # smarter stuff.) -from __future__ import print_function -from builtins import object from traceback import print_exc from .stack import expression_to_string, stack_to_string from ..joy import joy -from ..library import inscribe, FunctionWrapper +from ..library import FunctionWrapper -@inscribe @FunctionWrapper def trace(stack, expression, dictionary): '''Evaluate a Joy expression on a stack and print a trace. @@ -114,10 +111,10 @@ class TracePrinter(object): if n > max_stack_length: max_stack_length = n lines.append((n, '%s • %s' % (stack, expression))) - return [ # Prefix spaces to line up '•'s. - (' ' * (max_stack_length - length) + line) - for length, line in lines - ] + for i in range(len(lines)): # Prefix spaces to line up '•'s. + length, line = lines[i] + lines[i] = (' ' * (max_stack_length - length) + line) + return lines def print_(self): try: -- 2.11.0