Add style argument to sprint_func_line function
- This allows for alternate formatting of the output produced by
sprint_func_line
Change-Id: Ia2e80195ec7f264a65117c24beab34ca153cf3cc
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/gen_print.py b/lib/gen_print.py
index ce7d0f8..8f97791 100755
--- a/lib/gen_print.py
+++ b/lib/gen_print.py
@@ -1076,7 +1076,11 @@
return buffer
-def sprint_func_line(stack_frame):
+func_line_style_std = None
+func_line_style_short = 1
+
+
+def sprint_func_line(stack_frame, style=None):
r"""
For the given stack_frame, return a formatted string containing the
function name and all its arguments.
@@ -1088,6 +1092,18 @@
Description of argument(s):
stack_frame A stack frame (such as is returned by
inspect.stack()).
+ style Indicates the style or formatting of the
+ result string. Acceptable values are
+ shown above.
+
+ Description of styles:
+ func_line_style_std The standard formatting.
+ func_line_style_short 1) The self parm (associated with methods)
+ will be dropped. 2) The args and kwargs
+ values will be treated as special. In
+ both cases the arg name ('args' or
+ 'kwargs') will be dropped and only the
+ values will be shown.
"""
func_name = str(stack_frame[3])
@@ -1109,9 +1125,22 @@
# Get the arg value from frame locals.
arg_value = locals[arg_name]
if arg_name == 'self':
+ if style == func_line_style_short:
+ continue
# Manipulations to improve output for class methods.
func_name = arg_value.__class__.__name__ + "." + func_name
args_list.append(arg_name + " = <self>")
+ elif (style == func_line_style_short
+ and arg_name == 'args'
+ and type(arg_value) in (list, tuple)):
+ if len(arg_value) == 0:
+ continue
+ args_list.append(repr(', '.join(arg_value)))
+ elif (style == func_line_style_short
+ and arg_name == 'kwargs'
+ and type(arg_value) is dict):
+ for key, value in arg_value.items():
+ args_list.append(key + "=" + repr(value))
else:
args_list.append(arg_name + " = " + repr(arg_value))
args_str = "(" + ', '.join(map(str, args_list)) + ")"
@@ -1123,7 +1152,8 @@
def sprint_call_stack(indent=0,
- stack_frame_ix=0):
+ stack_frame_ix=0,
+ style=None):
r"""
Return a call stack report for the given point in the program with line
numbers, function names and function parameters and arguments.
@@ -1146,6 +1176,8 @@
line of output.
stack_frame_ix The index of the first stack frame which
is to be returned.
+ style See the sprint_line_func prolog above for
+ details.
"""
buffer = ""
@@ -1170,7 +1202,7 @@
line_num = str(current_stack[ix + 1][2])
except IndexError:
line_num = ""
- func_and_args = sprint_func_line(stack_frame)
+ func_and_args = sprint_func_line(stack_frame, style=style)
buffer += sindent(format_string % (line_num, func_and_args), indent)
ix += 1
@@ -1180,7 +1212,7 @@
return buffer
-def sprint_executing(stack_frame_ix=None):
+def sprint_executing(stack_frame_ix=None, style=None):
r"""
Print a line indicating what function is executing and with what parameter
values. This is useful for debugging.
@@ -1198,6 +1230,8 @@
the caller is the wrapper function
"print_executing", this function will bump
it up by 1.
+ style See the sprint_line_func prolog above for
+ details.
"""
# If user wants default stack_frame_ix.
@@ -1211,7 +1245,7 @@
stack_frame = inspect.stack()[stack_frame_ix]
- func_and_args = sprint_func_line(stack_frame)
+ func_and_args = sprint_func_line(stack_frame, style)
return sprint_time() + "Executing: " + func_and_args + "\n"