Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/test_eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,31 @@ TEST_SUBMODULE(eval_, m) {
return false;
});

// Line numbers reported from evaluated source must match the source as given;
// before #6089 a prepended coding cookie shifted them by one.
m.def("test_eval_line_numbers", []() {
int syntax_lineno = -1;
try {
py::exec("x = 1\nx = = 2\n");
throw std::runtime_error("py::exec did not raise SyntaxError");
} catch (py::error_already_set &e) {
syntax_lineno = e.value().attr("lineno").cast<int>();
}
int traceback_lineno = -1;
try {
py::exec("x = 1\nraise RuntimeError('line two')\n");
throw std::runtime_error("py::exec did not raise RuntimeError");
} catch (py::error_already_set &e) {
py::object tb = e.trace();
for (py::object next = tb.attr("tb_next"); !next.is_none();
next = tb.attr("tb_next")) {
tb = std::move(next);
}
traceback_lineno = tb.attr("tb_lineno").cast<int>();
}
return py::make_tuple(syntax_lineno, traceback_lineno);
});

// test_eval_empty_globals
m.def("eval_empty_globals", [](py::object global) {
if (global.is_none()) {
Expand Down
4 changes: 4 additions & 0 deletions tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def test_eval_file():
assert m.test_eval_file_failure()


def test_eval_line_numbers():
assert m.test_eval_line_numbers() == (2, 2)


def test_eval_empty_globals():
assert "__builtins__" in m.eval_empty_globals(None)

Expand Down
Loading