gruns / icecream

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

We want to still print the given args even when the source is unavaii… #136

Closed ehudhala closed 1 year ago

ehudhala commented 2 years ago

A common usage scenario of icecream is debugging long running processes. As part of the debug process a common scenario is that the underlying source code is updated with new insights. The current behavior causes all the scheduled prints using ic() to fail and only show the following message:

ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution?

This fix understands that the user probably still wants to see their printed debug values even if icecream can't introspect the arguments themselves. After this PR is merged if we run:

from icecream import ic
from time import sleep
a, b = 1, 2
sleep(10)
ic(a, b)

And change the underlying source code during the sleep period, ic will still print the values:

ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution?: 1
    Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution?: 2

Since the default error message is really long and informative we want to allow users to configure the error message so that on the common scenario of a known source change when a user prints a value they can configure a short message that looks something like:

from icecream import ic
ic.configureOutput(noSourceAvailableMessage='Source file changed')
a = 1
eval('ic(a)')

# ic| Source file changed: 1
alexmojaki commented 1 year ago

I don't like the idea of solving this problem with more configuration. icecream already has many options, I don't the the API should be bloated further. This also isn't very convenient. I think changing the default behaviour would be better. I think:

  1. Still showing the values is a good improvement.
  2. The error shouldn't be shown more than once. It should either be implemented as a standard warning or it should behave similarly, only showing the first time.
  3. The error and the value should be separated, at least by a newline, so even if the message is long and the terminal doesn't wrap lines the value is still easily visible.
ehudhala commented 1 year ago

@alexmojaki Thanks for the suggestions! Originally I didn't want to change the behavior too much so I left ic printing the warning as the value but I like your suggestion a lot better. I've updated the PR to issue a warning using the standard python warnings module. After the update running the same code snippet and changing the source file during the sleep:

# foobar.py
from icecream import ic
from time import sleep
a, b = 1, 2
sleep(10)
ic(a, b)

Will result in the following output:

./foobar.py:5: RuntimeWarning: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution?
ic| 1, 2
alexmojaki commented 1 year ago

Code and behaviour seems good to me, thanks! @gruns any objections to this change?

gruns commented 1 year ago

@ehudhala

  1. awesome suggestion
  2. THANK YOU for your wonderful PR! 🙌

@alexmojaki

  1. thank you for the ping here to review
  2. i took a look through things and everything looks 👍 to me. no objections to merge. the honor is all yours 🙂
alexmojaki commented 1 year ago

You'll have to release though

lukelbd commented 1 year ago

I often run into this problem with long-running processes and was considering addressing it myself, then came across this awesome PR! Thanks for your hard work!

ehud-nym commented 10 months ago

Hey :) Pinging back here since we didn't end up releasing a new version with this update. I've been using my local clone of icecream but we should probably release this as a public build. @gruns is it possible for me to help somehow with the release process?