gruns / icecream

🍦 Never use print() to debug again.
MIT License
9.2k stars 186 forks source link

IceCreamDebugger now ignores exceptions in formatting #152

Open NightMachinery opened 1 year ago

NightMachinery commented 1 year ago

When using icecream with multiprocessing, I had this exception. So I simply added a try-catch-print to Icecream to avoid breaking the program when something unexpected happens, as the results of the print are for debugging purposes and don't change program behavior anyway.

This exception interrupted a longrunning computation, so I believe my PR is quite useful.

Traceback (most recent call last):                                                         
  File "/home/mehri/micromamba/envs/p310/lib/python3.10/multiprocessing/pool.py", line 125,
 in worker                                                                                 
    result = (True, func(*args, **kwds))                                                   
  File "/home/mehri/code/DecompV/decompv/x/ds/utils.py", line 2066, in pt_logits_to_cls_met
rics                                                                                       
  File "/home/mehri/code/DecompV/decompv/x/ds/utils.py", line 2044, in logits_to_cls_metric
s                                                                                          
    # ic(len(tds_masked_pred), len(tds_masked_pred.dataset), len(dataset_masked_logits))   
  File "/home/mehri/code/DecompV/decompv/x/ds/utils.py", line 2010, in compute_cls_metrics 
    ):                                                                                     
  File "/home/mehri/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py"
, line 208, in __call__                                                           
    out = self._format(callFrame, *args)                                                   
  File "/home/mehri/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py"
, line 242, in _format                                                                     
    out = self._formatArgs(                                                                
  File "/home/mehri/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py"
, line 255, in _formatArgs                                                                 
    out = self._constructArgumentOutput(prefix, context, pairs)                            
  File "/home/mehri/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py"
, line 304, in _constructArgumentOutput                                                    
    arg_lines = [                                                                          
  File "/home/mehri/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py"
, line 305, in <listcomp>                                                                  
    format_pair('', arg, value)                                                            
  File "/home/mehri/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py"
, line 146, in format_pair                                                                 
    arg_lines = indented_lines(prefix, arg)                                                
  File "/home/mehri/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py"
, line 139, in indented_lines                                                              
    return [prefix + lines[0]] + [                                                         
IndexError: list index out of range   
NightMachinery commented 8 months ago

@gruns Can you merge this in? I just had another run interrupted because I was editing the source code of my program (while the code was running) and icecream fails if it cannot parse the source code.

  File ~/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py:208 in __call__
    out = self._format(callFrame, *args)

  File ~/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py:242 in _format
    out = self._formatArgs(

  File ~/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py:249 in _formatArgs
    sanitizedArgStrs = [

  File ~/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py:250 in <listcomp>
    source.get_text_with_indentation(arg)

  File ~/micromamba/envs/p310/lib/python3.10/site-packages/icecream/icecream.py:120 in get_text_with_indentation
    result = self.asttokens().get_text(node)

  File ~/micromamba/envs/p310/lib/python3.10/site-packages/executing/executing.py:443 in asttokens
    self._asttokens = self.asttext().asttokens

  File ~/micromamba/envs/p310/lib/python3.10/site-packages/asttokens/asttokens.py:329 in asttokens
    tree=self.tree,

  File ~/micromamba/envs/p310/lib/python3.10/site-packages/asttokens/asttokens.py:319 in tree
    self._tree = ast.parse(self._text, self._filename)

  File ~/micromamba/envs/p310/lib/python3.10/ast.py:50 in parse
    return compile(source, filename, mode, flags,

  File ~/code/DecompV/decompv/x/ds/utils.py:2373
    else:
    ^
IndentationError: expected an indented block after 'elif' statement on line 2370