Why doesn't this plugin error give a stack trace?

42 views
Skip to first unread message

James Cook

unread,
Dec 8, 2020, 8:55:15 PM12/8/20
to bean...@googlegroups.com
I've noticed that sometimes when plugin code triggers a runtime error, I don't get a stack trace. Here's a made-up minimal-ish example:

from beancount.core import amount, data, number
__plugins__ = ("foo",)
def foo(entries, _options):
    return [data.Transaction(
        flag = "*",
        payee = None,
        narration = "",
        tags = "",
        links = set(),
        postings = [data.Posting("Equity:Foo", amount.Amount(number.D("10"), "CAD") + "X", None, None, None, None)]
    )], ()

If I run bean-check on a file that imports that plugin, I just get this output:

falsifian angel beancount $ PYTHONPATH=./plugins bean-check main.beancount  
<load>:0:       Error importing "foo": can only concatenate tuple (not "str") to tuple

On the other hand, if I replace the body of foo() with 'raise BaseException("error")', I get a nice stack trace:

falsifian angel beancount $ PYTHONPATH=./plugins bean-check main.beancount  
Traceback (most recent call last):
  File "/home/falsifian/.local/bin/bean-check", line 11, in <module>
    load_entry_point('beancount==2.3.3', 'console_scripts', 'bean-check')()
  File "/home/falsifian/.local/lib/python3.8/site-packages/beancount/scripts/check.py", line 49, in main
    entries, errors, _ = loader.load_file(
  File "/home/falsifian/.local/lib/python3.8/site-packages/beancount/loader.py", line 89, in load_file
    entries, errors, options_map = _load_file(
  File "/home/falsifian/.local/lib/python3.8/site-packages/beancount/loader.py", line 214, in wrapped
    result = function(toplevel_filename, *args, **kw)
  File "/home/falsifian/.local/lib/python3.8/site-packages/beancount/loader.py", line 255, in _uncached_loa
d_file
    return _load([(filename, True)], *args, **kw)
  File "/home/falsifian/.local/lib/python3.8/site-packages/beancount/loader.py", line 510, in _load
    entries, errors = run_transformations(entries, parse_errors, options_map,
  File "/home/falsifian/.local/lib/python3.8/site-packages/beancount/loader.py", line 588, in run_transform
ations
    entries, plugin_errors = callback(entries, options_map)
  File "/home/falsifian/w/colin_and_james/accounting/beancount/plugins/foo.py", line 4, in foo
    raise BaseException("error")
BaseException: error

Why does that happen? Is there a way I can get a stack trace more consistently? It would help me debug.

James

James Cook

unread,
Dec 8, 2020, 8:59:32 PM12/8/20
to bean...@googlegroups.com
I mostly answered my own question. If I really want a traceback, I can just catch the exception before foo() returns. Hacky example that uses traceback.format_exc() to show the stack trace:

from beancount.core import amount, data, number
import traceback

__plugins__ = ("foo",)
def foo(entries, _options):
    try:

        return [data.Transaction(
            flag = "*",
            payee = None,
            narration = "",
            tags = "",
            links = set(),
            postings = [data.Posting("Equity:Foo", amount.Amount(number.D("10"), "CAD") + "X", None, None, None, None)]
        )], ()
    except BaseException:
        traceback.format_exc()
        raise BaseException("error")

James

James Cook

unread,
Dec 8, 2020, 9:04:59 PM12/8/20
to bean...@googlegroups.com
Er, apparently I don't know what the above line does. I think the reason this worked for me is just that I changed the type of the exception to something beancount doesn't catch.

James Cook

unread,
Dec 8, 2020, 9:07:48 PM12/8/20
to bean...@googlegroups.com
One more try.
    print(traceback.format_exc())
is what I wanted in the "except" part.

Martin Blais

unread,
Dec 13, 2020, 6:45:52 PM12/13/20
to Beancount
That's indeed not consistent behavior and ought to be fixed.
I filed a ticket to look into it, ideally we'd want to get the same behavior.



--
You received this message because you are subscribed to the Google Groups "Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beancount+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/CAHpmPODhKOoEiPOLoMcMCWKyjF9BJjzVECZegXbhMn7SmCEu4Q%40mail.gmail.com.

Martin Blais

unread,
Dec 14, 2020, 1:19:56 AM12/14/20
to Martin Blais, Beancount
Reply all
Reply to author
Forward
0 new messages