Skip to content

Commit 2cb3534

Browse files
committed
Move filter_traceback to _pytest._code
1 parent 8e11fe5 commit 2cb3534

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

src/_pytest/_code/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .code import ExceptionInfo # noqa
55
from .code import Frame # noqa
66
from .code import Traceback # noqa
7+
from .code import filter_traceback # noqa
78
from .code import getrawcode # noqa
89
from .source import Source # noqa
910
from .source import compile_ as compile # noqa

src/_pytest/_code/code.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from inspect import CO_VARARGS, CO_VARKEYWORDS
77

88
import attr
9+
import pluggy
910
import re
1011
from weakref import ref
12+
import _pytest
1113
from _pytest.compat import _PY2, _PY3, PY35, safe_str
1214
from six import text_type
1315
import py
@@ -1007,3 +1009,36 @@ def is_recursion_error(excinfo):
10071009
return "maximum recursion depth exceeded" in str(excinfo.value)
10081010
except UnicodeError:
10091011
return False
1012+
1013+
1014+
# relative paths that we use to filter traceback entries from appearing to the user;
1015+
# see filter_traceback
1016+
# note: if we need to add more paths than what we have now we should probably use a list
1017+
# for better maintenance
1018+
1019+
_PLUGGY_DIR = py.path.local(pluggy.__file__.rstrip("oc"))
1020+
# pluggy is either a package or a single module depending on the version
1021+
if _PLUGGY_DIR.basename == "__init__.py":
1022+
_PLUGGY_DIR = _PLUGGY_DIR.dirpath()
1023+
_PYTEST_DIR = py.path.local(_pytest.__file__).dirpath()
1024+
_PY_DIR = py.path.local(py.__file__).dirpath()
1025+
1026+
1027+
def filter_traceback(entry):
1028+
"""Return True if a TracebackEntry instance should be removed from tracebacks:
1029+
* dynamically generated code (no code to show up for it);
1030+
* internal traceback from pytest or its internal libraries, py and pluggy.
1031+
"""
1032+
# entry.path might sometimes return a str object when the entry
1033+
# points to dynamically generated code
1034+
# see https://bitbucket.org/pytest-dev/py/issues/71
1035+
raw_filename = entry.frame.code.raw.co_filename
1036+
is_generated = "<" in raw_filename and ">" in raw_filename
1037+
if is_generated:
1038+
return False
1039+
# entry.path might point to a non-existing file, in which case it will
1040+
# also return a str object. see #1133
1041+
p = py.path.local(entry.path)
1042+
return (
1043+
not p.relto(_PLUGGY_DIR) and not p.relto(_PYTEST_DIR) and not p.relto(_PY_DIR)
1044+
)

src/_pytest/config/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import _pytest.hookspec # the extension point definitions
2020
import _pytest.assertion
2121
from pluggy import PluginManager, HookimplMarker, HookspecMarker
22+
from _pytest._code import ExceptionInfo, filter_traceback
2223
from _pytest.compat import safe_str
2324
from .exceptions import UsageError, PrintHelp
2425
from .findpaths import determine_setup, exists
@@ -57,15 +58,11 @@ def main(args=None, plugins=None):
5758
try:
5859
config = _prepareconfig(args, plugins)
5960
except ConftestImportFailure as e:
60-
from _pytest._code import ExceptionInfo
61-
6261
exc_info = ExceptionInfo(e.excinfo)
6362
tw = py.io.TerminalWriter(sys.stderr)
6463
tw.line(
6564
"ImportError while loading conftest '{e.path}'.".format(e=e), red=True
6665
)
67-
from _pytest.python import filter_traceback
68-
6966
exc_info.traceback = exc_info.traceback.filter(filter_traceback)
7067
exc_repr = (
7168
exc_info.getrepr(style="short", chain=False)

src/_pytest/python.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from _pytest.config import hookimpl
1717

1818
import _pytest
19-
import pluggy
19+
from _pytest._code import filter_traceback
2020
from _pytest import fixtures
2121
from _pytest import nodes
2222
from _pytest import deprecated
@@ -46,37 +46,6 @@
4646
)
4747
from _pytest.warning_types import RemovedInPytest4Warning, PytestWarning
4848

49-
# relative paths that we use to filter traceback entries from appearing to the user;
50-
# see filter_traceback
51-
# note: if we need to add more paths than what we have now we should probably use a list
52-
# for better maintenance
53-
_pluggy_dir = py.path.local(pluggy.__file__.rstrip("oc"))
54-
# pluggy is either a package or a single module depending on the version
55-
if _pluggy_dir.basename == "__init__.py":
56-
_pluggy_dir = _pluggy_dir.dirpath()
57-
_pytest_dir = py.path.local(_pytest.__file__).dirpath()
58-
_py_dir = py.path.local(py.__file__).dirpath()
59-
60-
61-
def filter_traceback(entry):
62-
"""Return True if a TracebackEntry instance should be removed from tracebacks:
63-
* dynamically generated code (no code to show up for it);
64-
* internal traceback from pytest or its internal libraries, py and pluggy.
65-
"""
66-
# entry.path might sometimes return a str object when the entry
67-
# points to dynamically generated code
68-
# see https://bitbucket.org/pytest-dev/py/issues/71
69-
raw_filename = entry.frame.code.raw.co_filename
70-
is_generated = "<" in raw_filename and ">" in raw_filename
71-
if is_generated:
72-
return False
73-
# entry.path might point to a non-existing file, in which case it will
74-
# also return a str object. see #1133
75-
p = py.path.local(entry.path)
76-
return (
77-
not p.relto(_pluggy_dir) and not p.relto(_pytest_dir) and not p.relto(_py_dir)
78-
)
79-
8049

8150
def pyobj_property(name):
8251
def get(self):

0 commit comments

Comments
 (0)