Kara-Moon Forum

Developers & Technology => Musical MIDI Accompaniment (MMA) => Topic started by: sciurius on December 13, 2019, 07:43:15 AM



Title: Crash when no terminal width
Post by: sciurius on December 13, 2019, 07:43:15 AM
If MMA is run in an environment that is not associated with a terminal, it crashes with a weird error.

For example, when using Emacs, edit t.mma and then M-x compile RET mma -n t.mma RET .

Code:
Traceback (most recent call last):
  File "/home/jv/bin/mma", line 78, in <module>
    import MMA.main
  File "/home/jv/lib/mma/MMA/main.py", line 157, in <module>
    MMA.parse.parseFile(f)
  File "/home/jv/lib/mma/MMA/parse.py", line 91, in parseFile
    parse(f)
  File "/home/jv/lib/mma/MMA/parse.py", line 161, in parse
    simpleFuncs[action](l[1:])
  File "/home/jv/lib/mma/MMA/grooves.py", line 463, in allgrooves
    error("AllGrooves: '%s' cannot be applied like this." % action)
  File "/home/jv/lib/mma/MMA/common.py", line 94, in error
    prettyPrint("Error: %s %s %s" % (linno, file, msg))
  File "/home/jv/lib/mma/MMA/common.py", line 74, in prettyPrint
    for a in wrap(msg, termwidth, initial_indent='', subsequent_indent='    '):
  File "/usr/lib64/python2.7/textwrap.py", line 354, in wrap
    return w.wrap(text)
  File "/usr/lib64/python2.7/textwrap.py", line 329, in wrap
    return self._wrap_chunks(chunks)
  File "/usr/lib64/python2.7/textwrap.py", line 258, in _wrap_chunks
    raise ValueError("invalid width %r (must be > 0)" % self.width)
ValueError: invalid width -1 (must be > 0)

It took me some time to find the cause of the problem. In "common.py", lines 46-47

Code:
from MMA.termsize import getTerminalSize
termwidth = getTerminalSize()[0]-1

Apparently this can fail and will set termwidth to -1, causing the pretty printing to crash.

Even though this is a weird case, I'd suggest to add a safety net, e.g.

Code:
# having the term width is nice for pretty print error/warning
from MMA.termsize import getTerminalSize
termwidth = getTerminalSize()[0]-1
if termwidth < 0:
    termwidth = 80


Title: Re: Crash when no terminal width
Post by: bvdp on December 13, 2019, 04:36:59 PM
I have no idea why the termsize.py code is failing, but I suspect that emacs isn't passing it's underlying size ... but, in that case termsize should default to 80x24. It's not that important ... so I'm taking your (easy!) fix. But, rather than <1 should we, perhaps, use <32 or something and default to 80 in that case? I don't want the wrap() function to create other problems. So, I'm suggesting:

    if termwidth < 32:
       termwidth = 32

Thoughts?


Title: Re: Crash when no terminal width
Post by: sciurius on December 14, 2019, 02:53:13 PM
I think best is to stick to two cases.

If termwidth is set, apparently the user or the system wants it so, so try prettyprinting with the given width.

If termwidth is -1, do not prettyprint.

The change is in MMA/common.py:

Code:
def prettyPrint(msg):
    """ Simple formatter for error/warning messages."""

    if isinstance(msg, list):
        msg = ' '.join(msg)

    try:
        for a in wrap(msg, termwidth, initial_indent='', subsequent_indent='    '):
            bufferPrint(a)
    except:
        bufferPrint(msg)



Title: Re: Crash when no terminal width
Post by: bvdp on December 14, 2019, 04:26:44 PM
Yes, I agree. Patch applied.


Title: Re: Crash when no terminal width
Post by: sciurius on December 14, 2019, 05:08:00 PM
Thanks!