Raise exception from call area?

Suppose I have the following script:

def do_not_call_on_one(i):
    if i == 1:
        raise ValueError("You never listen.")

    print "Success!"

do_not_call_on_one(1)

During the extraction process, you will see the following trace:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    do_not_call_on_one(1)
  File "test.py", line 3, in do_not_call_on_one
    raise ValueError("You never listen.")
ValueError: You never listen.

Is there a way to manipulate the call stack so that the error is emitted from the line that actually causes the problem, for example ?:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    do_not_call_on_one(1)
ValueError: You never listen.

This will save the developer's time, which otherwise would have been wasted on checking the call stack, searching for the incorrectly used function, when the correct behavior can be determined in advance.

Is there anything in Python that allows an exception to use modified tracing?

Update

There are butins that replicate this functionality:

# In test.py:
int('a')

# Executing 'python test.py' yields:
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    int('a')
ValueError: invalid literal for int() with base 10: 'a'

Note. Tracking does not descend into a function int()to display a bunch of useless areas (especially useless raise ValueError).

+2
3

tl; dr: , , , .

, , , , , . , .

-, , , , . - , , . , , , , , - (, , , ) , .

-, , "" . , , . , ? " ", ? stacktraces .

, , , , , , .

, , , , , , , .

+3

, , , ( , , , , , print "Success!" , , , - sys.stdout = int?).


, fooobar.com/questions/739147/... - , .

+1

The following is displayed in the console:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in do_not_call_on_one
ValueError: You never listen.
0
source

All Articles