Skip to content

Commit a1df458

Browse files
committed
code: use properties for derived attributes, use slots
Make the objects more light weight. Remove unused properties.
1 parent a7e38c5 commit a1df458

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/_pytest/_code/code.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,13 @@
5454
class Code:
5555
"""Wrapper around Python code objects."""
5656

57+
__slots__ = ("raw",)
58+
5759
def __init__(self, rawcode) -> None:
5860
if not hasattr(rawcode, "co_filename"):
5961
rawcode = getrawcode(rawcode)
6062
if not isinstance(rawcode, CodeType):
6163
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
6564
self.raw = rawcode
6665

6766
def __eq__(self, other):
@@ -70,6 +69,14 @@ def __eq__(self, other):
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")

testing/code/test_excinfo.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)