Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 41b1431

Browse files
m4ns0urtrotterdylan
authored andcommitted
Implement sys._getframe (#362)
1 parent 28097f2 commit 41b1431

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

lib/sys.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,13 @@ def exc_info():
7171

7272
def exit(code=None): # pylint: disable=redefined-builtin
7373
raise SystemExit(code)
74+
75+
76+
def _getframe(depth=0):
77+
f = __frame__()
78+
while depth > 0 and f is not None:
79+
f = f.f_back
80+
depth -= 1
81+
if f is None:
82+
raise ValueError('call stack is not deep enough')
83+
return f

lib/sys_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ def TestExitInvalidArgs():
8686
assert False
8787

8888

89+
def TestGetFrame():
90+
try:
91+
sys._getframe(42, 42)
92+
except TypeError:
93+
pass
94+
else:
95+
assert False
96+
try:
97+
sys._getframe(2000000000)
98+
except ValueError:
99+
pass
100+
else:
101+
assert False
102+
assert sys._getframe().f_code.co_name == '_getframe'
103+
assert sys._getframe(1).f_code.co_name == 'TestGetFrame'
104+
105+
89106
if __name__ == '__main__':
90107
# This call will incidentally test sys.exit().
91108
weetest.RunTests()

0 commit comments

Comments
 (0)