This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: OS X test for Tk availability in doesn't work
Type: behavior Stage: needs patch
Components: macOS, Tests, Tkinter Versions: Python 3.8, Python 3.7, Python 3.6, Python 3.4, Python 3.5, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: ronaldoussoren Nosy List: alex, glyph, jesstess, markroseman, ned.deily, ronaldoussoren
Priority: normal Keywords: patch

Created on 2013-03-20 19:50 by alex, last changed 2022-04-11 14:57 by admin.

File name Uploaded Description Edit
tk-cond.diff alex, 2013-03-20 19:50 review
issue-17496.txt ronaldoussoren, 2013-04-23 12:30 review
Messages (14)
msg184784 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2013-03-20 19:50
If I run:

$ python -mtest.test_tk

I get a skip, after speaking with people familiar with OS X, it appears that the condition for the skip uses old Carbon APIs, which are totally deprecated under 64-bit. Attached is a patch which should work.
msg184843 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2013-03-21 05:07
The test for the condition was added to solve the problem reported in Issue8716.  The Tk crash for test_ttk_guionly reported there still occurs on a current 10.8 system with the Apple-supplied Cocoa Tk under the same conditions, that is, when running the tests from a process without a window manager connection, like an ssh or buildbot process where the user is not also currently logged in as the main "GUI user".  And the skip test code in question does prevent that crash.  The skip test also works correctly when using a 64-bit framework Python build (./configure --enable-framework), i.e. it does not skip the tests in a normal terminal session.  A side effect of a framework build is that the Python interpreter runs within an OS X app bundle, which gives it magic GUI powers.

But if run from a non-framework (standard unix) build, the tests are skipped with a "cannot run without OS X gui process" skip although stubbing out the check, as your patch does, shows that test_tk and test_ttk_guionly appear to run without error.  So it seems that the skip test is too restrictive but it shouldn't be unilaterally deleted.
msg184844 - (view) Author: Glyph Lefkowitz (glyph) (Python triager) Date: 2013-03-21 05:22
Hi Ned,

It seems from your comment that you didn't read the patch.  Alex added a simpler check via launchctl, rather than by framework symbol groveling :).  He didn't remove the check.

It should be functionally identical to what's there now, but much shorter and without having to depend on ctypes.

msg184845 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2013-03-21 06:02
Um, yes, my tired eyes did skip over those added lines. Thanks, Glyph, and sorry, Alex.

While the suggested change solves the issue for the non-framework build case, it appears to introduce new problems.  For one, with the current skip test, it is possible to run the tests from a buildbot or ssh process as long as the user name is logged in.  That no longer works with the proposed change.  Also, "launchctl managername" does not appear to be available on releases prior to 10.6, releases we still support.
msg184846 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2013-03-21 06:14
Granted, the current test is a kludge.  We could make it a bigger kludge by trying launchctl first and if it fails move on to the current ctypes-based tests.  Any better options?
msg184850 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2013-03-21 06:49
A small helper program that does the equavalent of this should also work:

import Cocoa
Cocoa.NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(((10, 10), (100, 100)), 0, 0, 0)

If this code raises an exception you cannot create windows, if it doesn't you can. This doesn't actually show the window and shouldn't mess up the users desktop when running the testsuite interactively.
msg184851 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2013-03-21 06:53
Wouldn't it be better to check for the actual problem, that is use subprocess to start a small Tcl script that creates a window and check if that crashes?

That way the code for disabling the test doesn't have to try to guess whether or not Tk will crash in the current environment, and tests won't get skipped when the Tk folks get their act together and don't crash when the window manager isn't available.
msg184862 - (view) Author: Glyph Lefkowitz (glyph) (Python triager) Date: 2013-03-21 10:16
> Wouldn't it be better to check for the actual problem, that is use subprocess to start a small Tcl script that creates a window and check if that crashes?

Yes, this sounds great.  Doing it with Tcl means that we're not invoking any of the problematic code in question.  It sounds like this check could be done on other platforms as well, in lieu of looking for e.g. $DISPLAY.  If a tcl script can make tk windows, so should a Python script be able to.
msg187632 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2013-04-23 12:30
The attached patch start wish and stops it by sending the 'exit' command. With the patch I can run the tk tests without getting a skip. I cannot easily test if the patch also does the right thing on buildbots, but have high hopes.   I did check that just starting 'wish' on a machine where I don't have console access causes a crash (OSX 10.5, nobody on the console, I logged on through SSH).

Open issues with my patch:

* The check uses whatever version wish is on the PATH, which may or may
  not be related to the version of Tk that python is linked to. 

  It might be better to explicitly use /usr/bin/wish, as that one is
  known to be native port of Tk (not X11) and is known to crash. 

* Not tested if the patch causes a Skip when running tests without 
  GUI access.
msg193592 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2013-07-23 09:10
To respond to my own message: running a small Tcl script is not good enough, the process environment of python itself is also important.

In particular, GUI frameworks only work from inside an application bundle (or by using some undocumented private APIs) and that's why the Tk tests cannot work on OSX without installing. 

BTW. I just verified that MacOS.WMAvailable() works just fine in 64-bit processes (python 2.7 on OSX 10.8), and according to the header files the API functions used in its implementation (and the corresponding python code in 3.x) are not deprecated.
msg219025 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2014-05-24 05:43
(See also Issue18604 which has refactored this area.)
msg219028 - (view) Author: Jessica McKellar (jesstess) * (Python triager) Date: 2014-05-24 07:31
Some IRC discussion about what contributors should do while this is unresolved, and the bigger plan for comprehensively addressing this:

01:53 < ned_deily> jesstess, saw your nosy on Issue17496.  Beware that it's really a can of worms and definitely was misclassified as easy.  Lots of edge cases, some not discussed in the issue.
03:01 < jesstess> ned_deily: can you update the ticket with edge cases not yet discussed? The issue is making life hard for some of our interns, so I'd love to see some progress on it.
03:02 < jesstess> ned_deily: I also found ronaldoussoren's conclusions hard to follow. I think a re-statement of what the best solution for the problem is would be really helpful, if you have opinions on it.
03:04 < ned_deily> jesstess, can you say how it is causing problems?
03:08 < jesstess> ned_deily: They are working on Tkinter tickets, and their primary development platform happens to be OSX, where the tests are getting skipped. One has set up a Linux VM already, which is fine, but it's a confusing issue to hit for new contributors and they also want to have the confidence that changes and new tests pass on OSX before submitting patches for review.
03:09 < ned_deily> jesstess, OK.  Alas, Tk is a bit of a problem on any platform and it's pretty much a mess on OS X.
03:15 < ned_deily> jesstess, Currently, there are three different variants of Tk in use on OS X.  And the most commonly used variant, the Cocoa Tk 8.5, is still relatively new and has had major bugs (still present in the version shipped by Apple in OS X) and has variations from minor release to minor release.
03:20 < ned_deily> jesstess, The plan is to make things a lot easier to test by making it easier to use a custom-built Tk for OS X.  In the meantime, there really is no easy way around things.  Fixing #17496 by  itself won't allow testing on OS X. 
03:20 < ned_deily> jesstess, For the time being, I would suggest just testing as best as possible on whatever platforms one can and submitting the patches.
msg320500 - (view) Author: Mark Roseman (markroseman) * Date: 2018-06-26 15:48
Just to note, this remains a PITA. To run gui tests on macOS from a terminal window seems to require commenting out the SetFrontProcess() call. A better replacement is needed as noted in the previous discussion, as well this call was deprecated in OS X 10.9 (though has not yet been removed as of this writing).

In the interim, is there a precedent for adding another command line option to the 'test' module, e.g. '--forcemacgui' so that people who want to can run those tests from a clean checkout?
msg320551 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2018-06-27 07:17
Do the tests work properly from an installed python framework? 

That doesn't immediately help with the normal way of running tests, but might point to a way forward. The major difference with normal installs is that the python interpreter is started in a minimal .app bundle, which leads to better behaviour of Apple's GUI frameworks.
Date User Action Args
2022-04-11 14:57:43adminsetgithub: 61698
2018-06-27 07:17:07ronaldoussorensetmessages: + msg320551
2018-06-26 15:48:21markrosemansetnosy: + markroseman

messages: + msg320500
versions: + Python 3.6, Python 3.7, Python 3.8
2014-05-24 07:31:16jesstesssetmessages: + msg219028
2014-05-24 05:43:42ned.deilysetkeywords: - easy

messages: + msg219025
versions: + Python 3.5, - Python 3.2, Python 3.3
2014-05-24 05:35:57Jessica.McKellarsetnosy: + jesstess
2013-07-23 09:10:13ronaldoussorensetmessages: + msg193592
2013-04-23 12:30:22ronaldoussorensetfiles: + issue-17496.txt
messages: + msg187632

keywords: + easy
type: behavior
stage: needs patch
2013-03-21 10:16:47glyphsetmessages: + msg184862
2013-03-21 06:53:01ronaldoussorensetmessages: + msg184851
2013-03-21 06:49:29ronaldoussorensetmessages: + msg184850
2013-03-21 06:14:27ned.deilysetmessages: + msg184846
2013-03-21 06:02:21ned.deilysetmessages: + msg184845
2013-03-21 05:22:17glyphsetnosy: + glyph
messages: + msg184844
2013-03-21 05:07:37ned.deilysetversions: + Python 3.2, Python 3.3, Python 3.4
nosy: + ronaldoussoren, ned.deily

messages: + msg184843

assignee: ronaldoussoren
components: + macOS, Tests, Tkinter
2013-03-20 19:50:05alexcreate