Skip to content
This repository
Browse code

Don't crash PuDB when viewing an object with a buggy str() or repr().

  • Loading branch information...
commit 04a64ba43500dee8939067786deeff8ac82c8a3c 1 parent 4a090bf
P. Varet authored April 12, 2012

Showing 1 changed file with 16 additions and 9 deletions. Show diff stats Hide diff stats

  1. 25  pudb/var_view.py
25  pudb/var_view.py
@@ -198,16 +198,17 @@ def keypress(self, size, key):
198 198
 
199 199
 custom_stringifier_dict = {}
200 200
 
  201
+def type_stringifier(value):
  202
+    if HAVE_NUMPY and isinstance(value, numpy.ndarray):
  203
+        return "ndarray %s %s" % (value.dtype, value.shape)
  204
+    elif isinstance(value, STR_SAFE_TYPES):
  205
+        return str(value)
  206
+    else:
  207
+        return type(value).__name__
  208
+
201 209
 def get_stringifier(iinfo):
202 210
     if iinfo.display_type == "type":
203  
-        def _stringifier(value):
204  
-            if HAVE_NUMPY and isinstance(value, numpy.ndarray):
205  
-                return "ndarray %s %s" % (value.dtype, value.shape)
206  
-            elif isinstance(value, STR_SAFE_TYPES):
207  
-                return str(value)
208  
-            else:
209  
-                return type(value).__name__
210  
-        return _stringifier
  211
+        return type_stringifier
211 212
     elif iinfo.display_type == "repr":
212 213
         return repr
213 214
     elif iinfo.display_type == "str":
@@ -254,7 +255,13 @@ def walk_value(self, prefix, label, value, id_path=None, attr_prefix=None):
254 255
         elif isinstance(value, (str, unicode)):
255 256
             self.add_item(prefix, label, repr(value), id_path, attr_prefix)
256 257
         else:
257  
-            displayed_value = get_stringifier(iinfo)(value)
  258
+            try:
  259
+                displayed_value = get_stringifier(iinfo)(value)
  260
+            except Exception:
  261
+                ## Unfortunately, anything can happen when calling str() or
  262
+                ## repr() on a random object.
  263
+                displayed_value = type_stringifier(value) \
  264
+                                + " (!! %s error !!)" % iinfo.display_type
258 265
 
259 266
             self.add_item(prefix, label,
260 267
                 displayed_value, id_path, attr_prefix)

0 notes on commit 04a64ba

Please sign in to comment.
Something went wrong with that request. Please try again.