Skip to content

Conversation

@NightMachinery
Copy link

@NightMachinery NightMachinery commented Aug 4, 2023

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 NightMachinery changed the title IceCreamDebugger ignores exceptions IceCreamDebugger now ignores exceptions in formatting Aug 4, 2023
@NightMachinery NightMachinery marked this pull request as ready for review August 4, 2023 09:34
@NightMachinery
Copy link
Author

@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

@NightMachinery
Copy link
Author

bumping this

@gruns
Copy link
Owner

gruns commented Aug 4, 2025

@NightMachinery thank you for the bump! always appreciated 🙏

  1. we definitely want to prevent bombing and crashing long running processes !

  2. before merging and wrapping code in a help me understand how/when this error is thrown in more detail. in general, it is a code smell to wrap anything in a generic try: except: where all exceptions are throw and eaten

so! what behavior exactly causes these exceptions to be thrown? and then lets figure out the best way to catch those exceptions. because, ideally there's a way to avoid throwing those exceptions to begin with

that said, we also do want to prevent icecream from ever crashing your program. icecream should never do that. so perhaps a blanket, catch all try: catch is warranted here. or elsewhere

cc @Jakeroid for your thoughts here 🙌

@Jakeroid
Copy link
Collaborator

@gruns

I completely agree that icecream should not crash any code.

@NightMachinery Could you provide a code example where icecream does the crash/exceptions/etc?

@NightMachinery
Copy link
Author

I don't have a reproducible example, but this has happened several times to me so now I always install my own fork with this patch.

@Jakeroid
Copy link
Collaborator

@NightMachinery

Does your fork have the same patched code as this PR?

@NightMachinery
Copy link
Author

@Jakeroid Yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants