8000 macosx backend won't load · Issue #11846 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

macosx backend won't load #11846

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

Closed
jklymak opened this issue Aug 12, 2018 · 8 comments · Fixed by #11850
Closed

macosx backend won't load #11846

jklymak opened this issue Aug 12, 2018 · 8 comments · Fixed by #11850
Labels
GUI: MacOSX Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Milestone

Comments

@jklymak
Copy link
Member
jklymak commented Aug 12, 2018

Bug report

#11600 (3479481) appears to have broken the macosx backend.

% git checkout 34794819d8
% cd lib
% git clean -xdf
% cd ..
% pip install -e .
% pythonw testMac.py

Traceback (most recent call last):
  File "testMac.py", line 3, in <module>
    import matplotlib.pyplot as plt
  File "/Users/jklymak/matplotlib/lib/matplotlib/pyplot.py", line 2410, in <module>
    switch_backend(rcParams["backend"])
  File "/Users/jklymak/matplotlib/lib/matplotlib/pyplot.py", line 246, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "/Users/jklymak/anaconda3/envs/matplotlibdev/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/jklymak/matplotlib/lib/matplotlib/backends/backend_macosx.py", line 14, in <module>
    from matplotlib.backends import _macosx
ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
import matplotlib
matplotlib.use('macosx')

No error on the previous commit (f4ae5d1). #11600 had a bunch of changes in the _src/macosx.h....

@jklymak jklymak added Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. GUI: MacOSX labels Aug 12, 2018
@jklymak jklymak added this to the v3.0 milestone Aug 12, 2018
@anntzer
8000
Copy link
Contributor
anntzer commented Aug 12, 2018

In _macosx.m verify_framework, there is a block that reads

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSRunningApplication* app = [NSRunningApplication currentApplication];
    NSApplicationActivationPolicy activationPolicy = [app activationPolicy];
    [pool release];
    switch (activationPolicy) {
        case NSApplicationActivationPolicyRegular:
        case NSApplicationActivationPolicyAccessory:
            return true;
        case NSApplicationActivationPolicyProhibited:
            break;
    }

This block is present both in 2.2.3 and in 3.0.0rc1.

Can you printf the value of activationPolicy in both versions when you try to import the module?

@jklymak
Copy link
Member Author
jklymak commented Aug 13, 2018

printing as a %d, activationPolicy is -1 in v3.0.0rc1 and doesn't trigger the break or return below.

In v2.2.3 it activationPolicy is 0, and returns true out of the ActivationPolicyAccessory case.

Sorry, I have no idea what [app activationPolicy] does, or I could try and fix, but right now its broken on 3.0, and works 2.2.3...

@jklymak
Copy link
Member Author
jklymak commented Aug 13, 2018

I think NSApp = [NSApplication sharedApplication]; moved into lazy_init but verify_framework() needs the app to have started?

UPDATE: Yep - if I move verify_framework() into lazy_init() the Framwork version works fine. OTOH if i use the non-framework python I get a segfault instead of the nice error message, primarily because I don't know how to pass the fact that the verify_framework failed back to something at a higher level.

@anntzer
Copy link
Contributor
anntzer commented Aug 13, 2018

Just jotting down some notes:

  • [foo bar] is basically objC for foo.bar().
  • Looks like the issue was known since when the code came in: Fix for issue4372 #4452 (comment).
  • We can't afford to start an app unless we're going to commit to the osx backed because otherwise trying to use another backend can cause a segfault when the other GUI toolkit tries itself to start an app (that was caught by the unit test that went in backend switching -- don't create a public fallback API #11600 and is what motivated the refactor in _macosx.m to start with)

@jklymak
Copy link
Member Author
jklymak commented Aug 13, 2018

That all sounds correct. I think the check should go into lazy_init, but lazy_init needs to return something in failure and the higher functions need to bail on failure so the python interpreter can throw the error. I wasn’t familiar enough with the c++ to get that to happen.

@anntzer
Copy link
Contributor
anntzer commented Aug 13, 2018

Also, does it work if you switch back to using the osx<10.6 #ifdef?

@jklymak
Copy link
Member Author
jklymak commented Aug 13, 2018

Yep, that works fine - if I remove the ifdefs and just have:

static bool verify_framework(void)
{
    ProcessSerialNumber psn;
    if (CGMainDisplayID()!=0
     && GetCurrentProcess(&psn)==noErr
     && SetFrontProcess(&psn)==noErr) return true;
    PyErr_SetString(PyExc_ImportError,
        "Python is not installed as a framework. The Mac OS X backend will "
        "not be able to function correctly if Python is not installed as a "
        "framework. See the Python documentation for more information on "
        "installing Python as a framework on Mac OS X. Please either reinstall "
        "Python as a framework, or try one of the other backends. If you are "
        "using (Ana)Conda please install python.app and replace the use of "
        "'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the "
        "Matplotlib FAQ for more information.");
    return false;
}

and if I'm using pythonw it works, and if I just use python it gives the correct error message about needing to use the framework. Why are both versions of the check in there?

@dstansby
Copy link
Member

Feel free to ping me in pull requests if anyone wants someone with a mac to test stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GUI: MacOSX Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
0