@@ -38,8 +38,9 @@ def get_caller_source_location(
38
38
you higher (i.e., more end-user-facing) in the stack. Same behaviour as the `stacklevel` argument in
39
39
warnings.warn().
40
40
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.
43
44
44
45
Returns: A tuple of:
45
46
(filename, lineno, code_context)
@@ -177,7 +178,11 @@ def add_line() -> None:
177
178
return source
178
179
179
180
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 :
181
186
"""
182
187
Gets the source code of wherever this function is called.
183
188
@@ -189,6 +194,10 @@ def get_caller_source_code(stacklevel: int = 1, strip_lines: bool = False) -> st
189
194
you higher (i.e., more end-user-facing) in the stack. Same behaviour as the `stacklevel` argument in
190
195
warnings.warn().
191
196
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
+
192
201
strip_lines: A boolean flag about whether or not to strip leading and trailing whitespace off each line of a
193
202
multi-line function call. See the built-in string method `str.strip()` for behaviour.
194
203
@@ -197,7 +206,8 @@ def get_caller_source_code(stacklevel: int = 1, strip_lines: bool = False) -> st
197
206
"""
198
207
199
208
filename , lineno , code_context = get_caller_source_location (
200
- stacklevel = stacklevel + 1
209
+ stacklevel = stacklevel + 1 ,
210
+ truncate_stacklevel = truncate_stacklevel ,
201
211
)
202
212
203
213
return get_source_code_from_location (
0 commit comments