Don't let module imports have side-effects.

The pretty printer module was inscribing the trace command as a side-
effect of importing it.
This commit is contained in:
Simon Forman 2020-12-19 23:53:04 -08:00
parent 5a2758b50d
commit 1cf82b8bcb
3 changed files with 8 additions and 12 deletions

View File

@ -17,12 +17,12 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Thun. If not see <http://www.gnu.org/licenses/>. # along with Thun. If not see <http://www.gnu.org/licenses/>.
# #
from __future__ import print_function from .library import initialize, inscribe
from .library import initialize
from .joy import repl from .joy import repl
from .utils import pretty_print # Inscribe trace command. from .utils.pretty_print import trace
inscribe(trace)
print('''\ print('''\
Thun - Copyright © 2017 Simon Forman Thun - Copyright © 2017 Simon Forman
This program comes with ABSOLUTELY NO WARRANTY; for details type "warranty". This program comes with ABSOLUTELY NO WARRANTY; for details type "warranty".

View File

@ -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. match the behaviour of the original version(s) written in C.
''' '''
from __future__ import print_function
from builtins import input from builtins import input
from traceback import print_exc from traceback import print_exc
from .parser import text_to_expression, ParseError, Symbol from .parser import text_to_expression, ParseError, Symbol

View File

@ -38,15 +38,12 @@ the pending expression to the right.
''' '''
# (Kinda clunky and hacky. This should be swapped out in favor of much # (Kinda clunky and hacky. This should be swapped out in favor of much
# smarter stuff.) # smarter stuff.)
from __future__ import print_function
from builtins import object
from traceback import print_exc from traceback import print_exc
from .stack import expression_to_string, stack_to_string from .stack import expression_to_string, stack_to_string
from ..joy import joy from ..joy import joy
from ..library import inscribe, FunctionWrapper from ..library import FunctionWrapper
@inscribe
@FunctionWrapper @FunctionWrapper
def trace(stack, expression, dictionary): def trace(stack, expression, dictionary):
'''Evaluate a Joy expression on a stack and print a trace. '''Evaluate a Joy expression on a stack and print a trace.
@ -114,10 +111,10 @@ class TracePrinter(object):
if n > max_stack_length: if n > max_stack_length:
max_stack_length = n max_stack_length = n
lines.append((n, '%s%s' % (stack, expression))) lines.append((n, '%s%s' % (stack, expression)))
return [ # Prefix spaces to line up '•'s. for i in range(len(lines)): # Prefix spaces to line up '•'s.
(' ' * (max_stack_length - length) + line) length, line = lines[i]
for length, line in lines lines[i] = (' ' * (max_stack_length - length) + line)
] return lines
def print_(self): def print_(self):
try: try: