Reduce numpy array traceback print
* changed numpy array traceback print
* added checkbox to reduced array formatting
* Revert "added checkbox to reduced array formatting"
This reverts commit
* reverting changes that added the checkbox
import re
from typing import Callable, Dict, Generator
+import numpy as np
+
from ..types import ExcInfo
@@ -27,6 +29,12 @@ def get_tb_formatter() -> Callable[[ExcInfo, bool], str]:
def format_exc_info(
info: ExcInfo, as_html: bool, color='Neutral'
) -> str:
+ # avoids printing the array data
+ # some discussion related to obtaining the current string function
+ # can be found here, https://github.com/numpy/numpy/issues/11266
+ np.set_string_function(
+ lambda arr: f'{type(arr)} {arr.shape} {arr.dtype}'
+ )
vbtb = IPython.core.ultratb.VerboseTB(color_scheme=color)
if as_html:
ansi_string = vbtb.text(*info).replace(" ", "&nbsp;")
@@ -37,9 +45,12 @@ def get_tb_formatter() -> Callable[[ExcInfo, bool], str]:
+ html
+ "</span>"
)
- return html
+ tb_text = html
else:
- return vbtb.text(*info)
+ tb_text = vbtb.text(*info)
+ # resets to default behavior
+ np.set_string_function(None)
+ return tb_text
except ImportError:
import cgitb
@@ -70,6 +81,10 @@ def get_tb_formatter() -> Callable[[ExcInfo, bool], str]:
return cgitb.html(info)
def format_exc_info(info: ExcInfo, as_html: bool, color=None) -> str:
+ # avoids printing the array data
+ np.set_string_function(
+ lambda arr: f'{type(arr)} {arr.shape} {arr.dtype}'
+ )
if as_html:
html = "\n".join(cgitb_chain(info[1]))
# cgitb has a lot of hardcoded colors that don't work for us
@@ -97,10 +112,13 @@ def get_tb_formatter() -> Callable[[ExcInfo, bool], str]:
+ html
+ "</span>"
)
- return html
+ tb_text = html
else:
# if we don't need HTML, just use traceback
- return ''.join(traceback.format_exception(*info))
+ tb_text = ''.join(traceback.format_exception(*info))
+ # resets to default behavior
+ np.set_string_function(None)
+ return tb_text
return format_exc_info
