Skip to content

Commit a95da7a

Browse files
authored
Merge pull request #7980 from bluetech/code-changes
code: a few minor improvements
2 parents 7fb0ea3 + 531416c commit a95da7a

File tree

7 files changed

+89
-70
lines changed

7 files changed

+89
-70
lines changed

src/_pytest/_code/code.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,29 @@
5454
class Code:
5555
"""Wrapper around Python code objects."""
5656

57-
def __init__(self, rawcode) -> None:
58-
if not hasattr(rawcode, "co_filename"):
59-
rawcode = getrawcode(rawcode)
60-
if not isinstance(rawcode, CodeType):
61-
raise TypeError(f"not a code object: {rawcode!r}")
62-
self.filename = rawcode.co_filename
63-
self.firstlineno = rawcode.co_firstlineno - 1
64-
self.name = rawcode.co_name
65-
self.raw = rawcode
57+
__slots__ = ("raw",)
58+
59+
def __init__(self, obj: CodeType) -> None:
60+
self.raw = obj
61+
62+
@classmethod
63+
def from_function(cls, obj: object) -> "Code":
64+
return cls(getrawcode(obj))
6665

6766
def __eq__(self, other):
6867
return self.raw == other.raw
6968

7069
# Ignore type because of https://github.com/python/mypy/issues/4266.
7170
__hash__ = None # type: ignore
7271

72+
@property
73+
def firstlineno(self) -> int:
74+
return self.raw.co_firstlineno - 1
75+
76+
@property
77+
def name(self) -> str:
78+
return self.raw.co_name
79+
7380
@property
7481
def path(self) -> Union[py.path.local, str]:
7582
"""Return a path object pointing to source code, or an ``str`` in
@@ -117,12 +124,26 @@ class Frame:
117124
"""Wrapper around a Python frame holding f_locals and f_globals
118125
in which expressions can be evaluated."""
119126

127+
__slots__ = ("raw",)
128+
120129
def __init__(self, frame: FrameType) -> None:
121-
self.lineno = frame.f_lineno - 1
122-
self.f_globals = frame.f_globals
123-
self.f_locals = frame.f_locals
124130
self.raw = frame
125-
self.code = Code(frame.f_code)
131+
132+
@property
133+
def lineno(self) -> int:
134+
return self.raw.f_lineno - 1
135+
136+
@property
137+
def f_globals(self) -> Dict[str, Any]:
138+
return self.raw.f_globals
139+
140+
@property
141+
def f_locals(self) -> Dict[str, Any]:
142+
return self.raw.f_locals
143+
144+
@property
145+
def code(self) -> Code:
146+
return Code(self.raw.f_code)
126147

127148
@property
128149
def statement(self) -> "Source":
@@ -164,17 +185,20 @@ def getargs(self, var: bool = False):
164185
class TracebackEntry:
165186
"""A single entry in a Traceback."""
166187

167-
_repr_style: Optional['Literal["short", "long"]'] = None
168-
exprinfo = None
188+
__slots__ = ("_rawentry", "_excinfo", "_repr_style")
169189

170190
def __init__(
171191
self,
172192
rawentry: TracebackType,
173193
excinfo: Optional["ReferenceType[ExceptionInfo[BaseException]]"] = None,
174194
) -> None:
175-
self._excinfo = excinfo
176195
self._rawentry = rawentry
177-
self.lineno = rawentry.tb_lineno - 1
196+
self._excinfo = excinfo
197+
self._repr_style: Optional['Literal["short", "long"]'] = None
198+
199+
@property
200+
def lineno(self) -> int:
201+
return self._rawentry.tb_lineno - 1
178202

179203
def set_repr_style(self, mode: "Literal['short', 'long']") -> None:
180204
assert mode in ("short", "long")
@@ -1172,7 +1196,7 @@ def getfslineno(obj: object) -> Tuple[Union[str, py.path.local], int]:
11721196
obj = obj.place_as # type: ignore[attr-defined]
11731197

11741198
try:
1175-
code = Code(obj)
1199+
code = Code.from_function(obj)
11761200
except TypeError:
11771201
try:
11781202
fn = inspect.getsourcefile(obj) or inspect.getfile(obj) # type: ignore[arg-type]

src/_pytest/_code/source.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import inspect
33
import textwrap
44
import tokenize
5+
import types
56
import warnings
67
from bisect import bisect_right
78
from typing import Iterable
@@ -29,8 +30,11 @@ def __init__(self, obj: object = None) -> None:
2930
elif isinstance(obj, str):
3031
self.lines = deindent(obj.split("\n"))
3132
else:
32-
rawcode = getrawcode(obj)
33-
src = inspect.getsource(rawcode)
33+
try:
34+
rawcode = getrawcode(obj)
35+
src = inspect.getsource(rawcode)
36+
except TypeError:
37+
src = inspect.getsource(obj) # type: ignore[arg-type]
3438
self.lines = deindent(src.split("\n"))
3539

3640
def __eq__(self, other: object) -> bool:
@@ -122,19 +126,17 @@ def findsource(obj) -> Tuple[Optional[Source], int]:
122126
return source, lineno
123127

124128

125-
def getrawcode(obj, trycall: bool = True):
129+
def getrawcode(obj: object, trycall: bool = True) -> types.CodeType:
126130
"""Return code object for given function."""
127131
try:
128-
return obj.__code__
132+
return obj.__code__ # type: ignore[attr-defined,no-any-return]
129133
except AttributeError:
130-
obj = getattr(obj, "f_code", obj)
131-
obj = getattr(obj, "__code__", obj)
132-
if trycall and not hasattr(obj, "co_firstlineno"):
133-
if hasattr(obj, "__call__") and not inspect.isclass(obj):
134-
x = getrawcode(obj.__call__, trycall=False)
135-
if hasattr(x, "co_firstlineno"):
136-
return x
137-
return obj
134+
pass
135+
if trycall:
136+
call = getattr(obj, "__call__", None)
137+
if call and not isinstance(obj, type):
138+
return getrawcode(call, trycall=False)
139+
raise TypeError(f"could not get code object for {obj!r}")
138140

139141

140142
def deindent(lines: Iterable[str]) -> List[str]:

src/_pytest/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ def setup(self) -> None:
16481648

16491649
def _prunetraceback(self, excinfo: ExceptionInfo[BaseException]) -> None:
16501650
if hasattr(self, "_obj") and not self.config.getoption("fulltrace", False):
1651-
code = _pytest._code.Code(get_real_func(self.obj))
1651+
code = _pytest._code.Code.from_function(get_real_func(self.obj))
16521652
path, firstlineno = code.path, code.firstlineno
16531653
traceback = excinfo.traceback
16541654
ntraceback = traceback.cut(path=path, firstlineno=firstlineno)

testing/code/test_code.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,26 @@ def test_code_gives_back_name_for_not_existing_file() -> None:
2828
assert code.fullsource is None
2929

3030

31-
def test_code_with_class() -> None:
31+
def test_code_from_function_with_class() -> None:
3232
class A:
3333
pass
3434

35-
pytest.raises(TypeError, Code, A)
35+
with pytest.raises(TypeError):
36+
Code.from_function(A)
3637

3738

3839
def x() -> None:
3940
raise NotImplementedError()
4041

4142

4243
def test_code_fullsource() -> None:
43-
code = Code(x)
44+
code = Code.from_function(x)
4445
full = code.fullsource
4546
assert "test_code_fullsource()" in str(full)
4647

4748

4849
def test_code_source() -> None:
49-
code = Code(x)
50+
code = Code.from_function(x)
5051
src = code.source()
5152
expected = """def x() -> None:
5253
raise NotImplementedError()"""
@@ -73,7 +74,7 @@ def func() -> FrameType:
7374

7475

7576
def test_code_from_func() -> None:
76-
co = Code(test_frame_getsourcelineno_myself)
77+
co = Code.from_function(test_frame_getsourcelineno_myself)
7778
assert co.firstlineno
7879
assert co.path
7980

@@ -92,25 +93,25 @@ def test_code_getargs() -> None:
9293
def f1(x):
9394
raise NotImplementedError()
9495

95-
c1 = Code(f1)
96+
c1 = Code.from_function(f1)
9697
assert c1.getargs(var=True) == ("x",)
9798

9899
def f2(x, *y):
99100
raise NotImplementedError()
100101

101-
c2 = Code(f2)
102+
c2 = Code.from_function(f2)
102103
assert c2.getargs(var=True) == ("x", "y")
103104

104105
def f3(x, **z):
105106
raise NotImplementedError()
106107

107-
c3 = Code(f3)
108+
c3 = Code.from_function(f3)
108109
assert c3.getargs(var=True) == ("x", "z")
109110

110111
def f4(x, *y, **z):
111112
raise NotImplementedError()
112113

113-
c4 = Code(f4)
114+
c4 = Code.from_function(f4)
114115
assert c4.getargs(var=True) == ("x", "y", "z")
115116

116117

testing/code/test_excinfo.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def xyz():
147147
]
148148

149149
def test_traceback_cut(self):
150-
co = _pytest._code.Code(f)
150+
co = _pytest._code.Code.from_function(f)
151151
path, firstlineno = co.path, co.firstlineno
152152
traceback = self.excinfo.traceback
153153
newtraceback = traceback.cut(path=path, firstlineno=firstlineno)
@@ -290,7 +290,7 @@ def f():
290290
excinfo = pytest.raises(ValueError, f)
291291
tb = excinfo.traceback
292292
entry = tb.getcrashentry()
293-
co = _pytest._code.Code(h)
293+
co = _pytest._code.Code.from_function(h)
294294
assert entry.frame.code.path == co.path
295295
assert entry.lineno == co.firstlineno + 1
296296
assert entry.frame.code.name == "h"
@@ -307,7 +307,7 @@ def f():
307307
excinfo = pytest.raises(ValueError, f)
308308
tb = excinfo.traceback
309309
entry = tb.getcrashentry()
310-
co = _pytest._code.Code(g)
310+
co = _pytest._code.Code.from_function(g)
311311
assert entry.frame.code.path == co.path
312312
assert entry.lineno == co.firstlineno + 2
313313
assert entry.frame.code.name == "g"
@@ -747,7 +747,6 @@ def entry():
747747
from _pytest._code.code import Code
748748

749749
monkeypatch.setattr(Code, "path", "bogus")
750-
excinfo.traceback[0].frame.code.path = "bogus" # type: ignore[misc]
751750
p = FormattedExcinfo(style="short")
752751
reprtb = p.repr_traceback_entry(excinfo.traceback[-2])
753752
lines = reprtb.lines

0 commit comments

Comments
 (0)