Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: fix IPython's %pylab mode detection #25496

Merged
merged 1 commit into from Mar 20, 2023

Conversation

dnicolodi
Copy link
Contributor

@dnicolodi dnicolodi commented Mar 18, 2023

There is no pyplot module in the system registry, as the module is imported as matplotlib.pyplot.

This code tries to detect when matplotlib.pyplot.show() is invoked in a script run via the %run IPython's magic while in interactive mode as enabled by the %pylab or %matplotlib IPyhton's magic commands. In these modes, matplotlib.pyplot.show() should not block.

Failing do detect these modes, a simple test.py script as this

import matplotlib.pyplot as plt
plt.figure()
plt.show()

run in IPython as follows

%matplotlib
%run test.py

is not expected to block on matplotlib.pyplot.show(). Without this patch it does. Additionally, the redraw implemented by IPython at the end of %run caused an empty figure to be displayed when the script terminates.

Fixing the module lookup in sys.modules fixes the issue.

Fixes: 86f26a0.
Fixes: #25485.

@dnicolodi dnicolodi force-pushed the fix-ipython-block branch 2 times, most recently from e2a7b57 to e2dd681 Compare March 18, 2023 20:17
@dnicolodi
Copy link
Contributor Author

I've absolutely no idea why CircleCI does not want to run on this PR

@dnicolodi
Copy link
Contributor Author

Funny, it is expected that I need to authorize CircleCI on my GitHub account to have it run on PRs for matplotlib?

Copy link
Contributor

@greglucas greglucas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It helps to provide a little bit of context in the PR as well and not just a reference to the issue.

I can confirm that this works as expected now. There is no module pyplot in the system registry, it is matplotlib.pyplot as updated here. This looks like it was overlooked in the previous commit that used a from matplotlib import pyplot and then checked the pyplot attribute. 86f26a0

For other reviewers the reproducer is:

  1. Start ipython
  2. %matplotlib
  3. %run test.py
  4. You should be in interactive mode, not blocked

test.py file:

import matplotlib.pyplot as plt
plt.figure()
plt.show()

@greglucas greglucas added this to the v3.7.2 milestone Mar 19, 2023
@dnicolodi
Copy link
Contributor Author

Thanks @greglucas for the review. I haven't contributed to matplotlib much and different projects have different customs regarding the amount of detail to put into PR descriptions and commit message. Should I add more context to the commit message?

@greglucas
Copy link
Contributor

Should I add more context to the commit message?

It is nice for viewing git blames going back in history. For example, the one that this traced back through made it pretty easy for me to read what was going on: 86f26a0

Funny, it is expected that I need to authorize CircleCI on my GitHub account to have it run on PRs for matplotlib?

I don't think you should have to authorize anything to get CircleCI to run on a PR, maybe to get it to run on your own fork though... It looks like it did run eventually, so hopefully, it is good now.

@jklymak
Copy link
Member

jklymak commented Mar 19, 2023

Should I add more context to the commit message?

If you want reviewer attention it's a good idea to make it as easy as possible on them.

@dnicolodi
Copy link
Contributor Author

I don't think you should have to authorize anything to get CircleCI to run on a PR, maybe to get it to run on your own fork though... It looks like it did run eventually, so hopefully, it is good now.

It started running only after I authorized it. I think it may be a bug on the CircleCI side: a while back I tried CircleCI on a test project and when I understood that I was not going to use it after all I revoked the authorization for CircleCI from my GitHub projects.

@dnicolodi
Copy link
Contributor Author

Should I add more context to the commit message?

If you want reviewer attention it's a good idea to make it as easy as possible on them.

Do you realize that this comment is very dismissive of the time I spent in submitting a detailed bug report, provide feedback when other maintainers commented on such issue, track down the issue, and submit a patch to fix it. If you do realize that this attitude is not respectful of contributors, and this is the only contribution you want to give to this PR, please go play the burnt-out maintainer somewhere else.

@dnicolodi dnicolodi force-pushed the fix-ipython-block branch 2 times, most recently from 7569d18 to fffeb9b Compare March 19, 2023 20:33
There is no pyplot module in the system registry, as the module is
imported as matplotlib.pyplot.

This code tries to detect when matplotlib.pyplot.show() is invoked in
a script run via the %run IPython's magic while in interactive mode as
enabled by the %pylab or %matplotlib IPyhton's magic commands.  In these
modes, matplotlib.pyplot.show() should not block.

Failing do detect these modes, a simple test.py script as this

  import matplotlib.pyplot as plt
  plt.figure()
  plt.show()

run in IPython as follows

  %matplotlib
  %run test.py

is not expected to block on matplotlib.pyplot.show().  Without this
patch it does.  Additionally, the redraw implemented by IPython at the
end of %run caused an empty figure to be displayed when the script
terminates.

Fixing the module lookup in sys.modules fixes the issue.

Fixes: 86f26a0.
Fixes: matplotlib#25485.
@dnicolodi
Copy link
Contributor Author

@greglucas I updated the commit message.

@jklymak
Copy link
Member

jklymak commented Mar 19, 2023

My apologies - I wasn't trying to be dismissive at all. Just suggesting that a summary of the issue and why this is the correct fix would get it reviewed faster. Issues with lots of back and forth get confusing fast, and it's often not clear what the final proposed solution is.

@tacaswell tacaswell merged commit a7ecb9c into matplotlib:main Mar 20, 2023
31 checks passed
@tacaswell
Copy link
Member

Thanks for your work to debug and fix this @dnicolodi !

meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Mar 20, 2023
rcomer added a commit that referenced this pull request Mar 20, 2023
…496-on-v3.7.x

Backport PR #25496 on branch v3.7.x (BUG: fix IPython's %pylab mode detection)
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.

[Bug]: Main loop integration with IPyhton broken after matplotlib version 3.6.2
4 participants