pytest2/testing
Ran Benita d18cb961cf assertion/rewrite: fix internal error on collection error due to decorated function
For decorated functions, the lineno of the FunctionDef AST node points
to the `def` line, not to the first decorator line. On the other hand,
in code objects, the `co_firstlineno` points to the first decorator
line.

Assertion rewriting inserts some imports to code it rewrites. The
imports are inserted at the lineno of the first statement in the AST. In
turn, the code object compiled from the rewritten AST uses the lineno of
the first statement (which is the first inserted import).

This means that given a module like this,

```py
@foo
@bar
def baz(): pass
```

the lineno of the code object without assertion rewriting
(`--assertion=plain`) is 1, but with assertion rewriting it is 3.

And *this* causes some issues for the exception repr when e.g. the
decorator line is invalid and raises during collection. The code becomes
confused and crashes with

INTERNALERROR>   File "_pytest/_code/code.py", line 638, in get_source
INTERNALERROR>     lines.append(space_prefix + source.lines[line_index].strip())
INTERNALERROR> IndexError: list index out of range

Fix it by special casing decorators. Maybe there are other cases like
this but off hand I can't think of another Python construct where the
lineno of the item would be after its first line, and this is the only
such issue we have had reported.
2020-09-12 23:05:08 +03:00
..
code Fix INTERNALERROR when accessing locals / globals with faulty `exec` 2020-09-11 18:13:48 -07:00
example_scripts Demonstrate that plain unittest does not support async tests (#7607) 2020-08-04 19:37:41 -03:00
examples pre-commit run fix-encoding-pragma --all-files 2019-06-03 12:08:01 -03:00
freeze testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
io Merge pull request #7481 from bluetech/tw-unicode-escape 2020-07-11 16:01:45 -03:00
logging testing: fix flaky test when executed slowly 2020-08-23 12:32:30 +03:00
python Allow ovewriting a parametrized fixture while reusing the parent fixture's value 2020-09-11 16:53:34 -03:00
acceptance_test.py Remove broken pytest_collect_directory hook 2020-08-19 08:14:25 -03:00
conftest.py Change pytest deprecation warnings into errors for 6.0 release (#7362) 2020-07-22 21:36:51 -03:00
deprecated_test.py Reintroduce warnings postponed in 6.0 (#7637) 2020-08-22 11:17:50 -03:00
test_argcomplete.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_assertion.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_assertrewrite.py typing: set warn_unreachable 2020-08-04 09:59:46 +03:00
test_cacheprovider.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_capture.py capture: fix disabled()/global_and_fixture_disabled() enabling capturing when it was disabled 2020-08-16 23:21:45 +03:00
test_collection.py assertion/rewrite: fix internal error on collection error due to decorated function 2020-09-12 23:05:08 +03:00
test_compat.py Introduce --import-mode=importlib (#7246) 2020-06-13 11:29:01 -03:00
test_config.py Integrate warnings filtering directly into Config (#7700) 2020-09-04 11:57:15 -03:00
test_conftest.py Remove resultlog plugin 2020-08-19 08:14:28 -03:00
test_debugging.py Enable check_untyped_defs mypy option for testing/ too 2020-06-05 11:34:20 +03:00
test_doctest.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_entry_points.py Replace importlib_metadata with importlib.metadata on Python 3.8+ 2019-07-04 15:00:10 +02:00
test_error_diffs.py assertion: improve diff output of recursive dataclass/attrs 2020-07-08 19:04:56 +03:00
test_faulthandler.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_findpaths.py get_dirs_from_args handles paths with invalid syntax 2020-08-12 17:20:09 -03:00
test_helpconfig.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_junitxml.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_link_resolve.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_main.py Replace some usages of config.{rootdir,inifile} with config.{rootpath,inipath} 2020-09-04 18:05:42 +03:00
test_mark.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_mark_expression.py mark/expression: prevent creation of illegal Python identifiers 2020-06-05 15:57:48 +03:00
test_meta.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_monkeypatch.py typing: set warn_unreachable 2020-08-04 09:59:46 +03:00
test_nodes.py Remove no longer needed `noqa: F821` uses 2020-07-10 13:08:56 +03:00
test_nose.py Node.location: handle str with _node_location_to_relpath 2020-01-23 10:45:31 +01:00
test_parseopt.py testing: improve bash check 2020-07-21 21:21:09 +03:00
test_pastebin.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_pathlib.py pathlib: add analogues to py.path.local's bestrelpath and common 2020-08-06 18:16:04 +03:00
test_pluginmanager.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_pytester.py typing: set warn_unreachable 2020-08-04 09:59:46 +03:00
test_recwarn.py recwarn: improve return type annotation of non-contextmanager pytest.warns 2020-06-25 14:30:42 +03:00
test_reports.py Improve typing of reports' longrepr field 2020-08-04 22:52:24 +03:00
test_runner.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_runner_xunit.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_session.py Simplify test_rootdir_wrong_option_arg (#6812) 2020-02-28 18:38:07 +01:00
test_setuponly.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_setupplan.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_skipping.py testing: fix some docstring issues 2020-08-03 10:10:43 +03:00
test_stepwise.py testing: fix flaky tests on pypy3 due to resource warnings in stderr (#7405) 2020-06-26 09:50:19 -03:00
test_store.py Remove no longer needed `noqa: F821` uses 2020-07-10 13:08:56 +03:00
test_terminal.py Replace some usages of config.{rootdir,inifile} with config.{rootpath,inipath} 2020-09-04 18:05:42 +03:00
test_tmpdir.py Format docstrings in a consistent style 2020-08-01 17:14:37 +03:00
test_unittest.py Fix test_plain_unittest_does_not_support_async on pypy3 2020-08-05 15:24:08 -03:00
test_warning_types.py Make pytest warnings show as from 'pytest' module instead of '_pytest.warning_types' 2019-06-15 11:48:34 -03:00
test_warnings.py Integrate warnings filtering directly into Config (#7700) 2020-09-04 11:57:15 -03:00
typing_checks.py Fix typing of params ids callable form 2020-07-14 14:53:42 +03:00