MCPcopy Create free account
hub / github.com/pytest-dev/pytest / Code

Class Code

src/_pytest/_code/code.py:60–126  ·  view source on GitHub ↗

Wrapper around Python code objects.

Source from the content-addressed store, hash-verified

58
59
60class Code:
61 """Wrapper around Python code objects."""
62
63 __slots__ = ("raw",)
64
65 def __init__(self, obj: CodeType) -> None:
66 self.raw = obj
67
68 @classmethod
69 def from_function(cls, obj: object) -> "Code":
70 return cls(getrawcode(obj))
71
72 def __eq__(self, other):
73 return self.raw == other.raw
74
75 # Ignore type because of https://github.com/python/mypy/issues/4266.
76 __hash__ = None # type: ignore
77
78 @property
79 def firstlineno(self) -> int:
80 return self.raw.co_firstlineno - 1
81
82 @property
83 def name(self) -> str:
84 return self.raw.co_name
85
86 @property
87 def path(self) -> Union[Path, str]:
88 """Return a path object pointing to source code, or an ``str`` in
89 case of ``OSError`` / non-existing file."""
90 if not self.raw.co_filename:
91 return ""
92 try:
93 p = absolutepath(self.raw.co_filename)
94 # maybe don't try this checking
95 if not p.exists():
96 raise OSError("path check failed.")
97 return p
98 except OSError:
99 # XXX maybe try harder like the weird logic
100 # in the standard lib [linecache.updatecache] does?
101 return self.raw.co_filename
102
103 @property
104 def fullsource(self) -> Optional["Source"]:
105 """Return a _pytest._code.Source object for the full source file of the code."""
106 full, _ = findsource(self.raw)
107 return full
108
109 def source(self) -> "Source":
110 """Return a _pytest._code.Source object for the code object's source only."""
111 # return source only for that part of code
112 return Source(self.raw)
113
114 def getargs(self, var: bool = False) -> Tuple[str, ...]:
115 """Return a tuple with the argument names for the code object.
116
117 If 'var' is set True also return the names of the variable and

Callers 3

test_neFunction · 0.90
codeMethod · 0.85

Calls

no outgoing calls

Tested by 2

test_neFunction · 0.72