Skip to content

Commit 85bd108

Browse files
committed
docs and extending truncate_stacklevel to outer callers
1 parent 5fdbe4e commit 85bd108

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

aerosandbox/tools/inspect_tools.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ def get_caller_source_location(
3838
you higher (i.e., more end-user-facing) in the stack. Same behaviour as the `stacklevel` argument in
3939
warnings.warn().
4040
41-
truncate_stacklevel: If True, will truncate the stacklevel to the maximum possible value. This is useful if you
42-
41+
truncate_stacklevel: If True, will truncate the stacklevel to the maximum possible value. This
42+
allows handling of an error case where the supplied `stacklevel` argument exceeds the depth of the
43+
actual call stack.
4344
4445
Returns: A tuple of:
4546
(filename, lineno, code_context)
@@ -177,7 +178,11 @@ def add_line() -> None:
177178
return source
178179

179180

180-
def get_caller_source_code(stacklevel: int = 1, strip_lines: bool = False) -> str:
181+
def get_caller_source_code(
182+
stacklevel: int = 1,
183+
truncate_stacklevel: bool=False,
184+
strip_lines: bool = False
185+
) -> str:
181186
"""
182187
Gets the source code of wherever this function is called.
183188
@@ -189,6 +194,10 @@ def get_caller_source_code(stacklevel: int = 1, strip_lines: bool = False) -> st
189194
you higher (i.e., more end-user-facing) in the stack. Same behaviour as the `stacklevel` argument in
190195
warnings.warn().
191196
197+
truncate_stacklevel: If True, will truncate the stacklevel to the maximum possible value. This
198+
allows handling of an error case where the supplied `stacklevel` argument exceeds the depth of the
199+
actual call stack.
200+
192201
strip_lines: A boolean flag about whether or not to strip leading and trailing whitespace off each line of a
193202
multi-line function call. See the built-in string method `str.strip()` for behaviour.
194203
@@ -197,7 +206,8 @@ def get_caller_source_code(stacklevel: int = 1, strip_lines: bool = False) -> st
197206
"""
198207

199208
filename, lineno, code_context = get_caller_source_location(
200-
stacklevel=stacklevel + 1
209+
stacklevel=stacklevel + 1,
210+
truncate_stacklevel=truncate_stacklevel,
201211
)
202212

203213
return get_source_code_from_location(

0 commit comments

Comments
 (0)