1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import sys
import traceback
from ..codegen import make_function_with_signature
from ...tests.helper import pytest
def test_make_function_with_signature_lineno():
"""
Tests that a function made with ``make_function_with_signature`` is give
the correct line number into the module it was created from (i.e. the line
``make_function_with_signature`` was called from).
"""
def crashy_function(*args, **kwargs):
1 / 0
# Make a wrapper around this function with the signature:
# crashy_function(a, b)
# Note: the signature is not really relevant to this test
wrapped = make_function_with_signature(crashy_function, ('a', 'b'))
line = """
wrapped = make_function_with_signature(crashy_function, ('a', 'b'))
""".strip()
try:
wrapped(1, 2)
except Exception:
exc_cls, exc, tb = sys.exc_info()
assert exc_cls is ZeroDivisionError
# The *last* line in the traceback should be the 1 / 0 line in
# crashy_function; the next line up should be the line that the
# make_function_with_signature call was one
tb_lines = traceback.format_tb(tb)
assert '1 / 0' in tb_lines[-1]
assert line in tb_lines[-2] and 'line =' not in tb_lines[-2]
else:
pytest.fail('This should have caused an exception')
|