0

In numpy, when you make a mistake, the error doesn't tell you about all the numpy internals, just the user-level error made. For example:

import numpy as np
A = np.ones([1,2])
B = np.ones([2,3])
A+B

spits back

Traceback (most recent call last):
  File "/home/roderic/Desktop/scratchpad.py", line 5, in <module>
    A+B
ValueError: operands could not be broadcast together with shapes (1,2) (2,3) 

Notice how it doesn't tell you about all the internal bouncing around that numpy did in order to determine that you are multiplying incompatible matrices, nor where the ValueError was raised exactly. I want to do the same for my project, where the traceback should stop outside of the module internals (unless I am on debug mode). So, if the traceback is 10 steps long, and the first 4 are on user level, and the last 6 are internal processing from my library, I only want to feature the first 4.

I know how to extract the stack, but I don't know how to modify it and re-inject it before raising the exception. I also assume this is considered a bad idea, and if so, I'd like to know what my other options are.

My horrible temporary solution is looking like this:

    except AssertionError as error:
        # something went wrong, the input was not correct
        print( "Traceback (most recent call last):")
        for filepath, line_no, namespace, line in traceback.extract_stack():
            if os.path.basename(filepath)=='MyModuleName.py': break
            print(  '  File "{filepath}", line {line_no}, in {namespace}\n'
                    '    {line}'.format(**locals()))
        exit()
3
  • The easiest way is to simply catch the error and raise your own error. Actually modifying the stacktrace is not impossible but I wouldn't recommend it, here's an example though: github.com/mitsuhiko/jinja2/blob/master/jinja2/debug.py
    – Wolph
    Commented Jan 26, 2015 at 18:12
  • but my own error will only be raised after a ton of internal bouncing around. I want the user to never see any of that, bc they can't do anything with the info and there's a fair bit of it. I just want the user to know that his input to the library was wrong.
    – RodericDay
    Commented Jan 26, 2015 at 18:16
  • I see your problem, in that case I would suggest using the limit parameter to strip out unneeded information about the traceback. And if that's not an option than you can print a partial version of the stacktrace using print(traceback.format_list(your_filter_func(traceback.extract_stack()))) to make printing easier.
    – Wolph
    Commented Jan 26, 2015 at 18:22

1 Answer 1

1

The only reason that A+B doesn't show any internal stack frames is that numpy.ndarray.__add__() happens to be implemented in C, so there are no Python stack frames after the one containing the A+B to show. numpy is not doing anything special to clean up the stack trace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.