Skip to content

Mac OS X locale setup in thread happens sometime after run() is called #67985

Closed

Description

@barry-scott
BPO 23797
Nosy @barry-scott, @ronaldoussoren, @ned-deily, @bitdancer
Files
  • initLocale.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-10-23.19:34:57.196>
    created_at = <Date 2015-03-28.10:47:55.510>
    labels = ['OS-mac', 'type-bug']
    title = 'Mac OS X locale setup in thread happens sometime after run() is called'
    updated_at = <Date 2020-10-23.19:34:57.196>
    user = 'https://github.com/barry-scott'

    bugs.python.org fields:

    activity = <Date 2020-10-23.19:34:57.196>
    actor = 'ronaldoussoren'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-10-23.19:34:57.196>
    closer = 'ronaldoussoren'
    components = ['macOS']
    creation = <Date 2015-03-28.10:47:55.510>
    creator = 'barry-scott'
    dependencies = []
    files = ['38725']
    hgrepos = []
    issue_num = 23797
    keywords = []
    message_count = 6.0
    messages = ['239458', '239461', '239464', '239484', '379467', '379468']
    nosy_count = 4.0
    nosy_names = ['barry-scott', 'ronaldoussoren', 'ned.deily', 'r.david.murray']
    pr_nums = []
    priority = 'normal'
    resolution = 'works for me'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue23797'
    versions = ['Python 3.4']

    Activity

    1. barry-scott commented on Mar 28, 2015

      barry-scottmannequin
      MannequinAuthor

      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.

    2. bitdancer commented on Mar 28, 2015

      @bitdancer
      Member

      This is probably related to or effectively a duplicate of bpo-18378, which looks to be an Apple bug.

      Do you have the problem if you remove the getlocale() call?

    3. ned-deily commented on Mar 28, 2015

      @ned-deily
      Member

      The behavior noted in bpo-18378 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?

    4. barry-scott commented on Mar 29, 2015

      barry-scottmannequin
      MannequinAuthor

      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 bpo-18378 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.

    5. ronaldoussoren commented on Oct 23, 2020

      @ronaldoussoren
      Contributor

      I've been using the scriptlet below to try to attempt to reproduce the problem. I'm not sure if this matches the way the program of the OP is set up.

      The scriptlet below works for me without problems (macOS 10.15, Python 3.4 - 3.9).

      This either means the problem has been fixed in the meantime (if it was an OS bug), or my script doesn't reproduce the problematic setup.

      The traceback in the first message is due to LC_CTYPE=UTF-8 in the default shell environment on macOS, which wasn't recognised by locale.py for a long time. That issue has been fixed though.

      # ----

      import locale
      import threading
      
      def thread():
          print("thread locale:", locale.getlocale())
      
      locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
      #locale.getlocale()
      
      t = threading.Thread(target=thread)
      t.start()
      t.join()
    6. ronaldoussoren commented on Oct 23, 2020

      @ronaldoussoren
      Contributor

      I'm closing this issue as "works for me". Please re-open if there is more information on how to reproduce the problem.

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      OS-mactype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions