classification
Title: Mac OS X locale setup in thread happens sometime after run() is called
Type: Stage:
Components: macOS Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry-scott, ned.deily, r.david.murray, ronaldoussoren
Priority: normal Keywords:

Created on 2015-03-28 10:47 by barry-scott, last changed 2015-03-29 11:00 by barry-scott.

Files
File name Uploaded Description Edit
initLocale.py barry-scott, 2015-03-29 11:00
Messages (4)
msg239458 - (view) Author: Barry Alan Scott (barry-scott) Date: 2015-03-28 10:47
I'm seeing a random traceback when starting a new thread on Mac OS X 10.10.2 with python 3.4.3 final.

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/Users/barry/wc/svn/pysvn-phoenix/WorkBench/Source/wb_background_thread.py", line 40, in run
    self.app.log.info( 'BackgroundThread locale %r' % (locale.getlocale(),) )
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/locale.py", line 575, in getlocale
    return _parse_localename(localename)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/locale.py", line 484, in _parse_localename
    raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: UTF-8

Adding a time.sleep( 5 ) prevents the traceback.

Using _locale.setlocale( locale.LC_ALL, None ) to see the state with locale.py interfering it looks like the locale is being initialised in the thread after run() is called. (Make no sense to me, but I can reproduce).

I have failed to create a small script to show this bug.
msg239461 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-03-28 17:10
This is probably related to or effectively a duplicate of issue 18378, which looks to be an Apple bug.

Do you have the problem if you remove the getlocale() call?
msg239464 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2015-03-28 19:57
The behavior noted in Issue18378 isn't an Apple bug; it's a GNU vs BSD difference.  Can you avoid the problem by calling locale.getlocale() in the main program before creating any threads?
msg239484 - (view) Author: Barry Alan Scott (barry-scott) Date: 2015-03-29 11:00
I should point out using Python2.7 with wxPython I do not see this issue.
This issue was exposed during my efforts to port pysvn from py2.7+wxPython to py3.4+wxpython-phoenix.

I do setup locale on the main thread very early before starting the background thread.
I have attached the initLocale() I use.

The background thread needs the locale setup and I use getlocale() to prove that it is setup.

I do not see this as a duplicate of Issue18378 as I can init the locale. 

The problem, and I could believe its an OSX bug, is that new threads do not return the expected locale from _locale.setlocale() for a period of time, approx 2s after starting up.
History
Date User Action Args
2015-03-29 11:00:56barry-scottsetfiles: + initLocale.py

messages: + msg239484
2015-03-28 19:57:11ned.deilysetmessages: + msg239464
2015-03-28 17:10:28r.david.murraysetnosy: + ronaldoussoren, r.david.murray, ned.deily
messages: + msg239461
components: + macOS
2015-03-28 10:47:55barry-scottcreate