Skip to content

Commit

Permalink
Merge pull request inducer#429 from mvanderkamp/id_stringifier
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Feb 2, 2021
2 parents 350164e + 2fc4a3d commit da44ac3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
9 changes: 8 additions & 1 deletion pudb/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
\/enter/space - expand/collapse
h - collapse
l - expand
t/r/s/c - show type/repr/str/custom for this variable
t/r/s/i/c - show type/repr/str/id/custom for this variable
H - toggle highlighting
@ - toggle repetition at top
* - cycle attribute visibility: public/_private/__dunder__
Expand Down Expand Up @@ -922,6 +922,8 @@ def change_var_state(w, size, key):
iinfo.display_type = "repr"
elif key == "s":
iinfo.display_type = "str"
elif key == "i":
iinfo.display_type = "id"
elif key == "c":
iinfo.display_type = CONFIG["custom_stringifier"]
elif key == "H":
Expand Down Expand Up @@ -976,6 +978,8 @@ def edit_inspector_detail(w, size, key):
iinfo.display_type == "repr")
rb_show_str = urwid.RadioButton(rb_grp_show, "Show str()",
iinfo.display_type == "str")
rb_show_id = urwid.RadioButton(rb_grp_show, "Show id()",
iinfo.display_type == "id")
rb_show_custom = urwid.RadioButton(
rb_grp_show, "Show custom (set in prefs)",
iinfo.display_type == CONFIG["custom_stringifier"])
Expand Down Expand Up @@ -1025,6 +1029,8 @@ def edit_inspector_detail(w, size, key):
iinfo.display_type = "repr"
elif rb_show_str.get_state():
iinfo.display_type = "str"
elif rb_show_id.get_state():
iinfo.display_type = "id"
elif rb_show_custom.get_state():
iinfo.display_type = CONFIG["custom_stringifier"]

Expand Down Expand Up @@ -1072,6 +1078,7 @@ def insert_watch(w, size, key):
self.var_list.listen("t", change_var_state)
self.var_list.listen("r", change_var_state)
self.var_list.listen("s", change_var_state)
self.var_list.listen("i", change_var_state)
self.var_list.listen("c", change_var_state)
self.var_list.listen("H", change_var_state)
self.var_list.listen("@", change_var_state)
Expand Down
2 changes: 1 addition & 1 deletion pudb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def _update_config(check_box, new_state, option_newvalue):

# {{{ stringifier

stringifier_opts = ["type", "str", "repr"]
stringifier_opts = ["type", "str", "repr", "id"]
known_stringifier = conf_dict["stringifier"] in stringifier_opts
stringifier_rb_group = []
stringifier_edit = urwid.Edit(edit_text=conf_dict["custom_stringifier"])
Expand Down
17 changes: 12 additions & 5 deletions pudb/var_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ def type_stringifier(value):
return text_type(type(value).__name__)


def id_stringifier(obj):
return "{id:#x}".format(id=id(obj))


def error_stringifier(_):
return "ERROR: Invalid custom stringifier file."


def get_stringifier(iinfo):
"""Return a function that turns an object into a Unicode text object."""

Expand All @@ -330,17 +338,16 @@ def get_stringifier(iinfo):
return repr
elif iinfo.display_type == "str":
return str
elif iinfo.display_type == "id":
return id_stringifier
else:
try:
if not custom_stringifier_dict: # Only execfile once
from os.path import expanduser
execfile(expanduser(iinfo.display_type), custom_stringifier_dict)
except Exception:
print("Error when importing custom stringifier:")
from traceback import print_exc
print_exc()
raw_input("Hit enter:")
return lambda value: text_type("ERROR: Invalid custom stringifier file.")
ui_log.exception("Error when importing custom stringifier")
return error_stringifier
else:
if "pudb_stringifier" not in custom_stringifier_dict:
print("%s does not contain a function named pudb_stringifier at "
Expand Down
2 changes: 1 addition & 1 deletion test/test_var_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_get_stringifier():
A, A2, A(), A2(), u"lól".encode("utf8"), u"lól",
1233123, [u"lól".encode("utf8"), u"lól"],
] + numpy_values:
for display_type in ["type", "repr", "str"]:
for display_type in ["type", "repr", "str", "id"]:
iinfo = InspectInfo()
iinfo.display_type = display_type

Expand Down

0 comments on commit da44ac3

Please sign in to comment.